From 87e176f8827cef97766b82033e44d0c1b73945ff Mon Sep 17 00:00:00 2001 From: Jeremy Freudberg Date: Wed, 24 Jan 2018 21:35:56 +0000 Subject: [PATCH] Fix Flask error_handler_spec The error_handler_spec is supposed to be a dict of dict of dicts, but we had it as a dict of dict of functions, for unknown (historical?) reasons. Fix that, so that errors which occur in the Sahara API, but are not otherwise caught by `sahara.utils.api.Rest.route` get handled correctly by Flask instead of leading to some arcane error far down the stack. Change-Id: I1e9d5f3fa00308baa7eee101c1f3b5a666cae4aa Closes-Bug: #1745236 --- sahara/api/middleware/sahara_middleware.py | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sahara/api/middleware/sahara_middleware.py b/sahara/api/middleware/sahara_middleware.py index d70f2d2f5e..f9848a4cf0 100644 --- a/sahara/api/middleware/sahara_middleware.py +++ b/sahara/api/middleware/sahara_middleware.py @@ -56,16 +56,19 @@ def build_app(version_response=None): app.register_blueprint(api_v10.rest, url_prefix='/v1.1') app.register_blueprint(api_v11.rest, url_prefix='/v1.1') - def make_json_error(ex): - status_code = (ex.code - if isinstance(ex, werkzeug_exceptions.HTTPException) - else 500) - description = (ex.description - if isinstance(ex, werkzeug_exceptions.HTTPException) - else str(ex)) - return api_utils.render({'error': status_code, - 'error_message': description}, - status=status_code) + class _JSONErrorHandler(dict): + def __getitem__(self, ex): + status_code = (ex.code + if isinstance(ex, werkzeug_exceptions.HTTPException) + else 500) + description = (ex.description + if isinstance(ex, werkzeug_exceptions.HTTPException) + else str(ex)) + return api_utils.render({'error': status_code, + 'error_message': description}, + status=status_code) + + make_json_error = _JSONErrorHandler() for code in six.iterkeys(werkzeug_exceptions.default_exceptions): app.error_handler_spec[None][code] = make_json_error