diff options
author | Frederick Muriuki Muriithi | 2025-07-09 09:36:21 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-07-09 09:36:21 -0500 |
commit | 72fcbb0fc1e0c347c827042722c68ab5efbcfb2e (patch) | |
tree | be8752f36cda38c628d87f38e693e0d6e7824660 /gn_auth/errors/http/http_4xx_errors.py | |
parent | f703399bdda4d4f4362653bdea4190804c3590d5 (diff) | |
download | gn-auth-72fcbb0fc1e0c347c827042722c68ab5efbcfb2e.tar.gz |
Move error handling into separate package.
Diffstat (limited to 'gn_auth/errors/http/http_4xx_errors.py')
-rw-r--r-- | gn_auth/errors/http/http_4xx_errors.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/gn_auth/errors/http/http_4xx_errors.py b/gn_auth/errors/http/http_4xx_errors.py new file mode 100644 index 0000000..704f11b --- /dev/null +++ b/gn_auth/errors/http/http_4xx_errors.py @@ -0,0 +1,31 @@ +"""Handlers for HTTP 4** errors""" +import logging +from werkzeug.exceptions import NotFound + +from flask import request, jsonify, render_template + +from gn_auth.errors.tracing import add_trace + +__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 + } |