Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
make sure exception handling gets called on error, and also not on 404
Browse files Browse the repository at this point in the history
  • Loading branch information
sourtin committed Oct 5, 2020
1 parent 00f3ccc commit 2cac21d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lightbluetent/app.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 2cac21d

Please sign in to comment.