aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/errors.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-09-26 03:14:39 +0300
committerFrederick Muriuki Muriithi2023-09-26 03:44:34 +0300
commitac11c943c50633ed39f31688e78f9bcb933f78a7 (patch)
tree546d9ed13aff7e1df38110bb9c606d820b353684 /gn_auth/errors.py
parentd453f607ffa5d3a7c6dde31e16b0f4cbb7cbb069 (diff)
downloadgn-auth-ac11c943c50633ed39f31688e78f9bcb933f78a7.tar.gz
Handle AuthorisationError at the top-level
Add an error handler to gracefully handle the custom AuthorisationError at the application's top-level to avoid having to manually handle it everywhere that the error (and its sub-classes) might be raised.
Diffstat (limited to 'gn_auth/errors.py')
-rw-r--r--gn_auth/errors.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/gn_auth/errors.py b/gn_auth/errors.py
new file mode 100644
index 0000000..bd2c5eb
--- /dev/null
+++ b/gn_auth/errors.py
@@ -0,0 +1,20 @@
+"""Handle application level errors."""
+from flask import Flask, jsonify, current_app
+
+from gn_auth.auth.authorisation.errors import AuthorisationError
+
+def handle_authorisation_error(exc: AuthorisationError):
+ """Handle AuthorisationError if not handled anywhere else."""
+ current_app.logger.error(exc)
+ return jsonify({
+ "error": type(exc).__name__,
+ "error_description": " :: ".join(exc.args)
+ }), exc.error_code
+
+__error_handlers__ = {
+ AuthorisationError: handle_authorisation_error
+}
+def register_error_handlers(app: Flask):
+ """Register ALL defined error handlers"""
+ for klass, error_handler in __error_handlers__.items():
+ app.register_error_handler(klass, error_handler)