Nicer error formatting
This commit is contained in:
parent
229eeec1be
commit
4cea2a6449
1 changed files with 8 additions and 10 deletions
18
util/http.py
18
util/http.py
|
@ -2,31 +2,29 @@ import logging
|
|||
|
||||
from flask import request, abort as flask_abort, make_response
|
||||
from auth.auth import process_auth, extract_namespace_repo_from_session, get_authenticated_user, get_validated_token
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_MESSAGE = {}
|
||||
DEFAULT_MESSAGE[400] = 'Invalid Request'
|
||||
DEFAULT_MESSAGE[403] = 'Forbidden'
|
||||
DEFAULT_MESSAGE[403] = 'Permission Denied'
|
||||
DEFAULT_MESSAGE[404] = 'Not Found'
|
||||
|
||||
def abort(status_code, message=None, **kwargs):
|
||||
if status_code == 403 and not message:
|
||||
# Create a default error message for auth failure.
|
||||
message = 'Forbidden. '
|
||||
message = message % kwargs if message else DEFAULT_MESSAGE[status_code]
|
||||
|
||||
if status_code == 403:
|
||||
# Add the user information.
|
||||
auth_user = get_authenticated_user()
|
||||
auth_token = get_validated_token()
|
||||
if auth_user:
|
||||
message = message + 'Current user: ' + auth_user.username
|
||||
message = '%s (user: %s)' % (message, auth_user.username)
|
||||
elif auth_token:
|
||||
message = message + 'Current token: ' + auth_token.friendly_name or auth_token.code
|
||||
|
||||
message = message % kwargs if message else DEFAULT_MESSAGE[status_code]
|
||||
message = '%s (token: %s)' % (message, auth_token.friendly_name or auth_token.code)
|
||||
|
||||
# Log the abort.
|
||||
logger.error('Error %s: %s. Arguments: %s' % (status_code, message, request.view_args))
|
||||
|
||||
# Report the abort to the user.
|
||||
flask_abort(make_response(HTTPException(message), status_code, {}))
|
||||
flask_abort(make_response(message, status_code, {}))
|
||||
|
||||
|
|
Reference in a new issue