From 65197a6c91fe7cce62b981bb9d455a2329a80bfb Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Mon, 17 Oct 2016 17:41:39 +0100 Subject: [PATCH] handle Exception and remove duplication in errors.py ensure that if unexpected Exceptions are thrown, we handle them correctly (log and then return JSON) also remove some branches that will never trip, and combine a couple of identical functions --- app/errors.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/app/errors.py b/app/errors.py index 046f21690..3aa0b2831 100644 --- a/app/errors.py +++ b/app/errors.py @@ -42,10 +42,7 @@ def register_errors(blueprint): @blueprint.app_errorhandler(400) def bad_request(e): - if isinstance(e, str): - msg = e - else: - msg = e.description or "Invalid request parameters" + msg = e.description or "Invalid request parameters" current_app.logger.exception(msg) return jsonify(result='error', message=str(msg)), 400 @@ -61,10 +58,7 @@ def register_errors(blueprint): @blueprint.app_errorhandler(404) def page_not_found(e): - if isinstance(e, str): - msg = e - else: - msg = e.description or "Not found" + msg = e.description or "Not found" current_app.logger.exception(msg) return jsonify(result='error', message=msg), 404 @@ -73,18 +67,9 @@ def register_errors(blueprint): current_app.logger.exception(e) return jsonify(result='error', message=str(e.description)), 429 - @blueprint.app_errorhandler(500) - def internal_server_error(e): - current_app.logger.exception(e) - return jsonify(result='error', message="Internal server error"), 500 - @blueprint.app_errorhandler(NoResultFound) - def no_result_found(e): - current_app.logger.exception(e) - return jsonify(result='error', message="No result found"), 404 - @blueprint.app_errorhandler(DataError) - def data_error(e): + def no_result_found(e): current_app.logger.exception(e) return jsonify(result='error', message="No result found"), 404 @@ -101,3 +86,10 @@ def register_errors(blueprint): )]} ), 400 return jsonify(result='error', message="Internal server error"), 500 + + # this must be defined after all other error handlers since it catches the generic Exception object + @blueprint.app_errorhandler(500) + @blueprint.app_errorhandler(Exception) + def internal_server_error(e): + current_app.logger.exception(e) + return jsonify(result='error', message="Internal server error"), 500