aboutsummaryrefslogtreecommitdiff
path: root/qc_app/errors.py
blob: 06c5dd4bafde9876ad565041985bc5b3aea5375e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Application error handling."""
import logging
import traceback

import MySQLdb as mdb
from flask import Flask, render_template

def handle_general_exception(_exc: Exception):
    """Handle generic exceptions."""
    trace = traceback.format_exc()
    logging.error("Error: Generic unhandled exception!!\n%s", trace)
    return render_template("unhandled_exception.html", trace=trace)

def handle_general_mysqldb_errors(_exc: mdb.MySQLError):
    """Handle MySQLdb errors."""
    trace = traceback.format_exc()
    logging.error("MySQLError: Generic MySQL Error!!\n%s", trace)
    return render_template("unhandled_exception.html", trace=trace)

def register_error_handlers(app: Flask):
    """Register top-level error/exception handlers."""
    app.register_error_handler(Exception, handle_general_exception)
    app.register_error_handler(mdb.MySQLError, handle_general_mysqldb_errors)