Exception handling: include a random string to identify stacks from log

This commit is contained in:
TheAMM 2017-09-02 22:16:28 +03:00
parent 36d3f8aed0
commit 6b49af4fb7
2 changed files with 20 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import logging
import os
import string
import flask
from flask_assets import Bundle # noqa F401
@ -7,6 +8,7 @@ from flask_assets import Bundle # noqa F401
from nyaa.api_handler import api_blueprint
from nyaa.extensions import assets, db, fix_paginate, toolbar
from nyaa.template_utils import bp as template_utils_bp
from nyaa.utils import random_string
from nyaa.views import register_views
@ -46,9 +48,16 @@ def create_app(config):
if not app.config['DEBUG']:
@app.errorhandler(500)
def internal_error(exception):
app.logger.error(exception)
flask.flash(flask.Markup(
'<strong>An error occurred!</strong> Debug information has been logged.'), 'danger')
random_id = random_string(8, string.ascii_uppercase + string.digits)
# Pst. Not actually unique, but don't tell anyone!
app.logger.error('Exception occurred! Unique ID: %s', random_id, exc_info=exception)
markup_source = ' '.join([
'<strong>An error occurred!</strong>',
'Debug information has been logged.',
'Please pass along this ID: <kbd>{}</kbd>'.format(random_id)
])
flask.flash(flask.Markup(markup_source), 'danger')
return flask.redirect(flask.url_for('main.home'))
# Get git commit hash

View File

@ -1,5 +1,7 @@
import functools
import hashlib
import random
import string
from collections import OrderedDict
@ -22,6 +24,12 @@ def sorted_pathdict(input_dict):
return OrderedDict(sorted(directories.items()) + sorted(files.items()))
def random_string(length, charset=None):
if charset is None:
charset = string.ascii_letters + string.digits
return ''.join(random.choice(charset) for i in range(length))
def cached_function(f):
sentinel = object()
f._cached_value = sentinel