diff options
author | Frederick Muriuki Muriithi | 2024-01-29 04:54:37 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-29 07:32:58 +0300 |
commit | b1b2bfbc71faa65210a17de7b6da55b25506607f (patch) | |
tree | b177306bc3b6d0ff7d53f02d61b57401d69fb7fc | |
parent | 575119831dc107d9846050d9d99551a490ee1614 (diff) | |
download | gn-auth-b1b2bfbc71faa65210a17de7b6da55b25506607f.tar.gz |
Error handling: Handle 404 error.
-rw-r--r-- | gn_auth/errors.py | 18 | ||||
-rw-r--r-- | gn_auth/templates/404.html | 13 |
2 files changed, 29 insertions, 2 deletions
diff --git a/gn_auth/errors.py b/gn_auth/errors.py index bd2c5eb..52fc489 100644 --- a/gn_auth/errors.py +++ b/gn_auth/errors.py @@ -1,8 +1,21 @@ """Handle application level errors.""" -from flask import Flask, jsonify, current_app +from werkzeug.exceptions import NotFound +from flask import Flask, request, jsonify, current_app, render_template from gn_auth.auth.authorisation.errors import AuthorisationError +def page_not_found(exc): + """404 handler.""" + content_type = request.content_type + if bool(content_type) and content_type.lower() == "application/json": + return jsonify({ + "error": exc.name, + "error_description": (f"The page '{request.url}' does not exist on " + "this server.") + }), 404 + + return render_template("404.html", page=request.url) + def handle_authorisation_error(exc: AuthorisationError): """Handle AuthorisationError if not handled anywhere else.""" current_app.logger.error(exc) @@ -12,7 +25,8 @@ def handle_authorisation_error(exc: AuthorisationError): }), exc.error_code __error_handlers__ = { - AuthorisationError: handle_authorisation_error + AuthorisationError: handle_authorisation_error, + NotFound: page_not_found } def register_error_handlers(app: Flask): """Register ALL defined error handlers""" diff --git a/gn_auth/templates/404.html b/gn_auth/templates/404.html new file mode 100644 index 0000000..e17bfe8 --- /dev/null +++ b/gn_auth/templates/404.html @@ -0,0 +1,13 @@ +{%extends "base.html"%} + +{%block title%}404: Page Not Found{%endblock%} + +{%block pagetitle%}404: Could Not Find the Requested Page{%endblock%} + +{%block content%} + +<p> + The page "<strong>{{page}}</strong>" does not exist on this server. +</p> + +{%endblock%} |