about summary refs log tree commit diff
path: root/gn_auth/errors/__init__.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2025-07-09 11:51:37 -0500
committerFrederick Muriuki Muriithi2025-07-09 11:57:25 -0500
commit1740ccbe30946aa6693a6a9ed8211a2ff7cfbf3d (patch)
treeaf5b89ba28a39cd859ae943e8882f39f5ae54069 /gn_auth/errors/__init__.py
parente7a89061cba689268daf4991038d9bec763a06d3 (diff)
downloadgn-auth-1740ccbe30946aa6693a6a9ed8211a2ff7cfbf3d.tar.gz
Improve error handling and reporting.
- Refactor out common functionality into reusable utilities
- Handle errors from the Authlib library/package
- Handle 4xx errors generically.
Diffstat (limited to 'gn_auth/errors/__init__.py')
-rw-r--r--gn_auth/errors/__init__.py46
1 files changed, 16 insertions, 30 deletions
diff --git a/gn_auth/errors/__init__.py b/gn_auth/errors/__init__.py
index 533a842..97d1e9e 100644
--- a/gn_auth/errors/__init__.py
+++ b/gn_auth/errors/__init__.py
@@ -10,8 +10,9 @@ from flask import (Flask,
 
 from gn_auth.auth.errors import AuthorisationError
 
-from .tracing import add_trace
 from .http import http_error_handlers
+from .authlib import authlib_error_handlers
+from .common import add_trace, build_handler
 
 logger = logging.getLogger(__name__)
 
@@ -20,43 +21,28 @@ __all__ = ["register_error_handlers"]
 
 def handle_general_exception(exc: Exception):
     """Handle generic unhandled exceptions."""
-    logger.error("Error occurred!", exc_info=True)
-    content_type = request.content_type
-    if bool(content_type) and content_type.lower() == "application/json":
-        exc_args = [str(x) for x in exc.args]
-        msg = ("The following exception was raised while attempting to access "
-               f"{request.url}: {' '.join(exc_args)}")
-        return jsonify(add_trace(exc, {
-            "error": type(exc).__name__,
-            "error_description": msg
-        })), 500
-
-    return render_template("50x.html",
-                           page=request.url,
-                           error=exc,
-                           trace=traceback.format_exception(exc)), 500
+    exc_args = [str(x) for x in exc.args]
+    _handle = build_handler("A generic exception occurred: "
+                            " ".join(exc_args))
+    return _handle(exc)
 
 
 def handle_authorisation_error(exc: AuthorisationError):
     """Handle AuthorisationError if not handled anywhere else."""
-    logger.error("Error occurred!", exc_info=True)
-    logger.error(exc)
-    return jsonify(add_trace(exc, {
-        "error": type(exc).__name__,
-        "error_description": " :: ".join(exc.args)
-    })), exc.error_code
-
-__error_handlers__ = {
-    Exception: handle_general_exception,
-    AuthorisationError: handle_authorisation_error
-}
+    exc_args = [str(x) for x in exc.args]
+    _handle = build_handler("A generic authorisation error occurred: "
+                            " ".join(exc_args))
+    return _handle(exc)
 
 
 def register_error_handlers(app: Flask):
     """Register ALL defined error handlers"""
     _handlers = {
-        **__error_handlers__,
-        **http_error_handlers()
+        **authlib_error_handlers(),
+        **http_error_handlers(),
+        Exception: handle_general_exception,
+        AuthorisationError: handle_authorisation_error
     }
-    for class_, error_handler in __error_handlers__.items():
+    for class_, error_handler in _handlers.items():
+        logger.debug("Register handler for %s", class_.__name__)
         app.register_error_handler(class_, error_handler)