diff options
author | Frederick Muriuki Muriithi | 2024-01-30 07:27:53 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-30 07:27:53 +0300 |
commit | 10ec3b1f851897c2cce89356b4404c18f9f11a2f (patch) | |
tree | 693d2a29479b0c2bbee818edcce61fa7a32ac6fb | |
parent | b1b2bfbc71faa65210a17de7b6da55b25506607f (diff) | |
download | gn-auth-10ec3b1f851897c2cce89356b4404c18f9f11a2f.tar.gz |
Add exception traces to error outputs.
-rw-r--r-- | gn_auth/errors.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/gn_auth/errors.py b/gn_auth/errors.py index 52fc489..fdd2f3e 100644 --- a/gn_auth/errors.py +++ b/gn_auth/errors.py @@ -1,28 +1,37 @@ """Handle application level errors.""" +import traceback + from werkzeug.exceptions import NotFound from flask import Flask, request, jsonify, current_app, render_template from gn_auth.auth.authorisation.errors import AuthorisationError +def add_trace(exc: Exception, errobj: dict) -> dict: + """Add the traceback to the error handling object.""" + return { + **errobj, + "error-trace": "".join(traceback.format_exception(exc)) + } + def page_not_found(exc): """404 handler.""" content_type = request.content_type if bool(content_type) and content_type.lower() == "application/json": - return jsonify({ + return jsonify(add_trace(exc, { "error": exc.name, "error_description": (f"The page '{request.url}' does not exist on " "this server.") - }), 404 + })), 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) - return jsonify({ + return jsonify(add_trace(exc, { "error": type(exc).__name__, "error_description": " :: ".join(exc.args) - }), exc.error_code + })), exc.error_code __error_handlers__ = { AuthorisationError: handle_authorisation_error, |