From f7f45bbdd87bb3ee8f7841006012a6aef32d13da Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Tue, 10 Oct 2017 10:34:27 +0200 Subject: [PATCH] send admin email on exception --- ordr2/views/errors.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ordr2/views/errors.py b/ordr2/views/errors.py index b2ccf0e..eab3be9 100644 --- a/ordr2/views/errors.py +++ b/ordr2/views/errors.py @@ -1,5 +1,10 @@ ''' display error pages ''' +import io +import pprint +import traceback +import sys + from pyramid.exceptions import BadCSRFToken from pyramid.view import ( @@ -7,6 +12,8 @@ from pyramid.view import ( forbidden_view_config, view_config ) +from pyramid_mailer.message import Message + @notfound_view_config(renderer='ordr2:templates/errors/not_found.jinja2') @@ -38,6 +45,34 @@ def bad_csrf_view(context, request): renderer='ordr2:templates/errors/exception.jinja2' ) def exception_view(context, request): + + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback_output = io.StringIO() + traceback.print_exc(file=traceback_output) + + request_output = io.StringIO() + pprint.pprint(request.__dict__, stream=request_output) + + settings = request.registry.settings + default_sender = settings['mail.default_sender'] + recipient = settings.get('admin_email', None) or default_sender + + body = '\n'.join([ + traceback_output.getvalue(), + '', + '', + 'Request:', + request_output.getvalue() + ]) + + message = Message( + subject='[ordr] Exception occured', + sender=default_sender, + recipients=[recipient], + body=body + ) + request.mailer.send_immediately(message) + context.nav_highlight = 'errors' request.response.status = 500 return {}