2014-01-24 20:09:35 +00:00
|
|
|
import logging
|
2014-03-12 20:31:37 +00:00
|
|
|
import json
|
2014-01-24 20:09:35 +00:00
|
|
|
|
2014-04-07 05:20:09 +00:00
|
|
|
from app import analytics
|
2014-03-25 19:38:31 +00:00
|
|
|
from flask import request, abort as flask_abort, make_response, current_app
|
2014-02-25 20:07:24 +00:00
|
|
|
from auth.auth_context import get_authenticated_user, get_validated_token
|
2014-01-24 20:01:40 +00:00
|
|
|
|
2014-01-24 20:09:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
|
2014-01-24 20:01:40 +00:00
|
|
|
DEFAULT_MESSAGE = {}
|
|
|
|
DEFAULT_MESSAGE[400] = 'Invalid Request'
|
2014-01-24 20:26:32 +00:00
|
|
|
DEFAULT_MESSAGE[401] = 'Unauthorized'
|
2014-01-24 20:17:00 +00:00
|
|
|
DEFAULT_MESSAGE[403] = 'Permission Denied'
|
2014-01-24 20:01:40 +00:00
|
|
|
DEFAULT_MESSAGE[404] = 'Not Found'
|
2014-01-24 20:26:32 +00:00
|
|
|
DEFAULT_MESSAGE[409] = 'Conflict'
|
2014-01-28 23:29:45 +00:00
|
|
|
DEFAULT_MESSAGE[501] = 'Not Implemented'
|
2014-01-24 20:01:40 +00:00
|
|
|
|
2014-03-12 20:31:37 +00:00
|
|
|
def abort(status_code, message=None, issue=None, headers=None, **kwargs):
|
2014-02-25 19:15:12 +00:00
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
message = (str(message) % kwargs if message else
|
|
|
|
DEFAULT_MESSAGE.get(status_code, ''))
|
2014-01-24 20:17:00 +00:00
|
|
|
|
2014-01-25 01:29:25 +00:00
|
|
|
params = dict(request.view_args)
|
2014-01-25 01:33:42 +00:00
|
|
|
params.update(kwargs)
|
2014-01-25 01:29:25 +00:00
|
|
|
|
2014-01-25 01:40:22 +00:00
|
|
|
params['url'] = request.url
|
2014-01-25 01:37:44 +00:00
|
|
|
params['status_code'] = status_code
|
|
|
|
params['message'] = message
|
|
|
|
|
2014-01-24 20:26:32 +00:00
|
|
|
# Add the user information.
|
|
|
|
auth_user = get_authenticated_user()
|
|
|
|
auth_token = get_validated_token()
|
|
|
|
if auth_user:
|
2014-04-07 05:20:09 +00:00
|
|
|
analytics.track(auth_user.username, 'http_error', params)
|
2014-01-24 20:26:32 +00:00
|
|
|
message = '%s (user: %s)' % (message, auth_user.username)
|
|
|
|
elif auth_token:
|
2014-04-07 05:20:09 +00:00
|
|
|
analytics.track(auth_token.code, 'http_error', params)
|
2014-01-24 22:00:42 +00:00
|
|
|
message = '%s (token: %s)' % (message,
|
|
|
|
auth_token.friendly_name or auth_token.code)
|
2014-01-24 20:01:40 +00:00
|
|
|
|
|
|
|
# Log the abort.
|
2014-01-25 01:40:22 +00:00
|
|
|
logger.error('Error %s: %s; Arguments: %s' % (status_code, message, params))
|
2014-01-28 23:29:45 +00:00
|
|
|
|
|
|
|
# Calculate the issue URL (if the issue ID was supplied).
|
|
|
|
issue_url = None
|
|
|
|
if issue:
|
2014-01-29 20:38:25 +00:00
|
|
|
issue_url = 'http://docs.quay.io/issues/%s.html' % (issue)
|
2014-01-28 23:29:45 +00:00
|
|
|
|
|
|
|
# Create the final response data and message.
|
|
|
|
data = {}
|
2014-01-29 19:08:14 +00:00
|
|
|
data['error'] = message
|
|
|
|
|
2014-01-28 23:29:45 +00:00
|
|
|
if issue_url:
|
|
|
|
data['info_url'] = issue_url
|
|
|
|
|
2014-03-25 19:38:31 +00:00
|
|
|
if headers is None:
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
# Add CORS headers to all errors
|
|
|
|
options_resp = current_app.make_default_options_response()
|
|
|
|
headers['Access-Control-Allow-Origin'] = '*'
|
|
|
|
headers['Access-Control-Allow-Methods'] = options_resp.headers['allow']
|
|
|
|
headers['Access-Control-Max-Age'] = str(21600)
|
|
|
|
headers['Access-Control-Allow-Headers'] = ['Authorization', 'Content-Type']
|
|
|
|
|
2014-03-12 20:31:37 +00:00
|
|
|
resp = make_response(json.dumps(data), status_code, headers)
|
2014-01-24 20:01:40 +00:00
|
|
|
|
|
|
|
# Report the abort to the user.
|
2014-01-24 22:00:42 +00:00
|
|
|
flask_abort(resp)
|