about summary refs log tree commit diff
path: root/gn_auth/errors/http
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/http
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/http')
-rw-r--r--gn_auth/errors/http/http_4xx_errors.py26
1 files changed, 9 insertions, 17 deletions
diff --git a/gn_auth/errors/http/http_4xx_errors.py b/gn_auth/errors/http/http_4xx_errors.py
index 704f11b..3a2ed88 100644
--- a/gn_auth/errors/http/http_4xx_errors.py
+++ b/gn_auth/errors/http/http_4xx_errors.py
@@ -1,31 +1,23 @@
 """Handlers for HTTP 4** errors"""
 import logging
-from werkzeug.exceptions import NotFound
 
-from flask import request, jsonify, render_template
+from werkzeug.exceptions import NotFound, Forbidden, Unauthorized
 
-from gn_auth.errors.tracing import add_trace
+from gn_auth.errors.common import build_handler
 
 __all__ = ["http_4xx_error_handlers"]
 
 logger = logging.getLogger(__name__)
 
-def page_not_found(exc):
-    """404 handler."""
-    logger.error("Page '%s' was not found.", request.url, exc_info=True)
-    content_type = request.content_type
-    if bool(content_type) and content_type.lower() == "application/json":
-        return jsonify(add_trace(exc, {
-            "error": exc.name,
-            "error_description": (f"The page '{request.url}' does not exist on "
-                                  "this server.")
-        })), exc.code
-
-    return render_template("404.html", page=request.url), exc.code
-
 
 def http_4xx_error_handlers() -> dict:
     """Return handlers for HTTP errors in the 400-499 range"""
     return {
-        NotFound: page_not_found
+        Forbidden: build_handler(
+            "You do not have the necessary privileges to access the requested "
+            "resource."),
+        NotFound: build_handler(
+            "The requested page does not exist on this server."),
+        Unauthorized: build_handler(
+            "You are not authorised to access the requested resource.")
     }