From e3724ffefcf432971da0c3500e15a22fcfbea790 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 20 Nov 2018 11:22:36 +0000 Subject: [PATCH] JSONified all HTTP errors --- openflexure_microscope/api/v1.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/openflexure_microscope/api/v1.py b/openflexure_microscope/api/v1.py index a3609b7d..07a7e2d0 100644 --- a/openflexure_microscope/api/v1.py +++ b/openflexure_microscope/api/v1.py @@ -17,6 +17,7 @@ from flask import ( make_response) from flask.views import MethodView +from werkzeug.exceptions import default_exceptions from openflexure_microscope.api.utilities import parse_payload, gen, get_bool @@ -45,10 +46,18 @@ def uri(suffix, base=None): app = Flask(__name__) # Make errors more API friendly -@app.errorhandler(404) -def not_found(error): - return make_response(jsonify({'error': 'Not found'}), 404) +def _handle_http_exception(e): + return make_response( + jsonify({ + 'status_code': e.code, + 'error': e.name, + 'details': e.description + }), + e.code) + +for code in default_exceptions: + app.errorhandler(code)(_handle_http_exception) # After app starts, but before first request, attach hardware to global microscope @app.before_first_request @@ -339,7 +348,6 @@ class CaptureListAPI(MicroscopeView): :