diff options
author | Frederick Muriuki Muriithi | 2024-03-04 11:05:52 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-03-04 11:08:15 +0300 |
commit | 48e9821b3ea31d27d2dd63a4fa7e304111ea1464 (patch) | |
tree | 9629eb7de402bf4c9e09dbcc043c2f31589733b5 /gn3 | |
parent | 36a2e10d2fe61c7c76e29938f7c35b92135655da (diff) | |
download | genenetwork3-48e9821b3ea31d27d2dd63a4fa7e304111ea1464.tar.gz |
Handle generic exceptions gracefully.
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/errors.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/gn3/errors.py b/gn3/errors.py index 245c791..7a9d0be 100644 --- a/gn3/errors.py +++ b/gn3/errors.py @@ -12,8 +12,8 @@ from SPARQLWrapper.SPARQLExceptions import ( Unauthorized ) from werkzeug.exceptions import NotFound -from flask import Flask, jsonify, current_app from authlib.oauth2.rfc6749.errors import OAuth2Error +from flask import Flask, jsonify, Response, current_app from gn3.auth.authorisation.errors import AuthorisationError @@ -93,9 +93,21 @@ def handle_sparql_errors(exc): }), __code.get(exc) +def handle_generic(exc: Exception) -> Response: + """Handle generic exception.""" + current_app.logger.error(exc) + resp = jsonify({ + "error": type(exc).__name__, + "error_description": exc.args[0], + }) + resp.status_code = 500 + return resp + + def register_error_handlers(app: Flask): """Register application-level error handlers.""" app.register_error_handler(NotFound, page_not_found) + app.register_error_handler(Exception, handle_generic) app.register_error_handler(OAuth2Error, handle_oauth2_errors) app.register_error_handler(OperationalError, handle_sqlite3_errors) app.register_error_handler(AuthorisationError, handle_authorisation_error) |