Browse Source

send admin email on exception

php2python
Holger Frey 7 years ago
parent
commit
f7f45bbdd8
  1. 35
      ordr2/views/errors.py

35
ordr2/views/errors.py

@ -1,5 +1,10 @@ @@ -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 ( @@ -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): @@ -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 {}