aboutsummaryrefslogtreecommitdiff
path: root/uploader/errors.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-07-25 11:07:33 -0500
committerFrederick Muriuki Muriithi2024-07-25 14:34:09 -0500
commit754e8f214b940e05298cb360ed829f5c685d55a5 (patch)
tree62c2c5b601746621f0949b38937ad232f006dee2 /uploader/errors.py
parentde9e1b9fe37928b864bea28b408de6c14d04526b (diff)
downloadgn-uploader-754e8f214b940e05298cb360ed829f5c685d55a5.tar.gz
Rename module: qc_app --> uploader
Diffstat (limited to 'uploader/errors.py')
-rw-r--r--uploader/errors.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/uploader/errors.py b/uploader/errors.py
new file mode 100644
index 0000000..3e7c893
--- /dev/null
+++ b/uploader/errors.py
@@ -0,0 +1,29 @@
+"""Application error handling."""
+import traceback
+from werkzeug.exceptions import HTTPException
+
+import MySQLdb as mdb
+from flask import Flask, request, render_template, current_app as app
+
+def handle_general_exception(exc: Exception):
+ """Handle generic exceptions."""
+ trace = traceback.format_exc()
+ app.logger.error(
+ "Error (%s.%s): Generic unhandled exception!! (URI: %s)\n%s",
+ exc.__class__.__module__, exc.__class__.__name__, request.url, trace)
+ return render_template("unhandled_exception.html", trace=trace), 500
+
+def handle_http_exception(exc: HTTPException):
+ """Handle HTTP exceptions."""
+ app.logger.error(
+ "HTTP Error %s: %s", exc.code, exc.description, exc_info=True)
+ return render_template("http-error.html",
+ request_url=request.url,
+ exc=exc,
+ trace=traceback.format_exception(exc)), exc.code
+
+def register_error_handlers(appl: Flask):
+ """Register top-level error/exception handlers."""
+ appl.register_error_handler(Exception, handle_general_exception)
+ appl.register_error_handler(HTTPException, handle_http_exception)
+ appl.register_error_handler(mdb.MySQLError, handle_general_exception)