From 4f152fd7c7d57261f91d68331d0776c5b6fbc975 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Sun, 8 Jul 2018 11:45:33 +0300 Subject: [PATCH] Make API errors more informative Fixes https://jira.coreos.com/browse/QUAY-999 --- endpoints/decorators.py | 9 +++++---- util/http.py | 9 ++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/endpoints/decorators.py b/endpoints/decorators.py index 787d5648d..bd16d1a8a 100644 --- a/endpoints/decorators.py +++ b/endpoints/decorators.py @@ -32,7 +32,7 @@ def parse_repository_name(include_tag=False, include_tag=include_tag, allow_library=features.LIBRARY_SUPPORT) except ImplicitLibraryNamespaceNotAllowed: - abort(400) + abort(400, message='A namespace must be specified explicitly') del kwargs[incoming_repo_kwarg] kwargs[ns_kwarg_name] = repo_name_components[0] @@ -54,7 +54,7 @@ def param_required(param_name, allow_body=False): def decorated(*args, **kwargs): if param_name not in request.args: if not allow_body or param_name not in request.values: - abort(make_response('Required param: %s' % param_name, 400)) + abort(400, message='Required param: %s' % param_name) return wrapped(*args, **kwargs) return decorated return wrapper @@ -85,7 +85,7 @@ def check_anon_protection(func): if get_authenticated_context() and not get_authenticated_context().is_anonymous: return func(*args, **kwargs) - abort(401) + abort(401, message='Anonymous access is not allowed') return wrapper @@ -117,7 +117,8 @@ def require_xhr_from_browser(func): if not has_xhr_header and not app.config.get('DEBUGGING') == True: logger.warning('Disallowed possible RTA to URL %s with user agent %s', request.path, request.user_agent) - abort(400) + abort(400, message='API calls must be invoked with an X-Requested-With header ' + + 'if called from a browser') return func(*args, **kwargs) return wrapper diff --git a/util/http.py b/util/http.py index 3ef44b235..bd3c854ec 100644 --- a/util/http.py +++ b/util/http.py @@ -19,7 +19,7 @@ DEFAULT_MESSAGE[409] = 'Conflict' DEFAULT_MESSAGE[501] = 'Not Implemented' -def _abort(status_code, data_object, headers): +def _abort(status_code, data_object, description, headers): # Add CORS headers to all errors options_resp = current_app.make_default_options_response() headers['Access-Control-Allow-Origin'] = '*' @@ -31,7 +31,7 @@ def _abort(status_code, data_object, headers): # Report the abort to the user. # Raising HTTPException as workaround for https://github.com/pallets/werkzeug/issues/1098 - new_exception = HTTPException(response=resp) + new_exception = HTTPException(response=resp, description=description) new_exception.code = status_code raise new_exception @@ -42,11 +42,10 @@ def exact_abort(status_code, message=None): if message is not None: data['error'] = message - _abort(status_code, data, {}) + _abort(status_code, data, message or None, {}) def abort(status_code, message=None, issue=None, headers=None, **kwargs): - message = (str(message) % kwargs if message else DEFAULT_MESSAGE.get(status_code, '')) @@ -80,4 +79,4 @@ def abort(status_code, message=None, issue=None, headers=None, **kwargs): if headers is None: headers = {} - _abort(status_code, data, headers) + _abort(status_code, data, message, headers)