From 2cac21d997c79f58956756c38183633fd99f5ec5 Mon Sep 17 00:00:00 2001 From: William Earley Date: Mon, 5 Oct 2020 12:16:42 +0100 Subject: [PATCH] make sure exception handling gets called on error, and also not on 404 --- lightbluetent/app.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lightbluetent/app.py b/lightbluetent/app.py index 0c0c4f2..fed0996 100644 --- a/lightbluetent/app.py +++ b/lightbluetent/app.py @@ -1,12 +1,14 @@ import os, subprocess, logging import logging.handlers from flask import Flask +from werkzeug.exceptions import HTTPException from . import rooms, users, society, admins, general from .flask_seasurf import SeaSurf from flask_talisman import Talisman from flask_babel import Babel from .utils import gen_unique_string, ordinal, sif, page_not_found, server_error, table_exists from lightbluetent.models import db, migrate, Setting, Role, Permission, User +from functools import wraps import click @@ -32,14 +34,29 @@ def configure_logging(app): @app.errorhandler(Exception) def exception_interceptor(e): + if isinstance(e, HTTPException): + code = e.code + if (code // 100) == 3: + # redirection, this probably never gets called though... + return e.get_response(request.environ), code app.logger.exception(e) - raise e + return server_error(e) @app.route('/oops') def oops(): 1/0 +def logging_wrapper(log_fun): + def decorator(f): + @wraps(f) + def wrapped(e, *a, **k): + log_fun(e) + return f(e, *a, **k) + return wrapped + return decorator + + def create_app(config_name=None): if config_name == None: @@ -80,8 +97,8 @@ def create_app(config_name=None): app.register_blueprint(users.bp) app.register_blueprint(society.bp) app.register_blueprint(admins.bp) - app.register_error_handler(404, page_not_found) - app.register_error_handler(500, server_error) + app.register_error_handler(404, logging_wrapper(app.logger.info)(page_not_found)) + app.register_error_handler(500, logging_wrapper(app.logger.exception)(server_error)) @app.context_processor def inject_gh_rev():