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

Commit

Permalink
Send mail on error with stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstaro committed Apr 19, 2013
1 parent 531ca55 commit 0bd0e36
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions flamejam/__init__.py
Expand Up @@ -5,6 +5,7 @@
from flask.ext.markdown import Markdown
from flask.ext.principal import Principal, Permission, RoleNeed
from flask.ext.login import LoginManager, current_user
from flask_errormail import mail_on_500

app = Flask(__name__)

Expand All @@ -21,6 +22,8 @@
login_manager.init_app(app)
login_manager.login_view = "login"

mail_on_500(app, app.config['ADMINS'])

principals = Principal(app)
admin_permission = Permission(RoleNeed('admin'))

Expand Down
68 changes: 68 additions & 0 deletions flamejam/flask_errormail.py
@@ -0,0 +1,68 @@
"""
flask_errormail
~~~~~~~~~~~~~~~
Flask extension for sending emails to administrators when 500 Internal
Server Errors occur.
:copyright: (c) 2012 by Jason Wyatt Feinstein.
:license: MIT, see LICENSE.txt for more details.
"""
import traceback

def mail_on_500(app, recipients):
'''Main function for setting up Flask-ErrorMail to send e-mails when 500
errors occur.
:param app: Flask Application Object
:type app: flask.Flask
:param recipients: List of recipient email addresses.
:type recipients: list or tuple
:param sender: Email address that should be listed as the sender. Defaults
to 'noreply@localhost'
:type sender: string
'''

#importing locally, so that the dependencies are only required if
# mail_on_500 is used.
from flask import request as __request
from flask import url_for as __url_for
from flask import redirect as __redirect
from flask_mail import Mail as __Mail
from flask_mail import Message as __Message

mail = __Mail(app)

# create a closure to track the recipients
def email_exception(exception):
'''Handles the exception message from Flask by sending an email to the
recipients defined in the call to mail_on_500.
'''

msg = __Message("[Flask|ErrorMail] Exception Detected: %s" % exception.message,
recipients=recipients)
msg_contents = [
'Traceback:',
'='*80,
traceback.format_exc(),
]
msg_contents.append('\n')
msg_contents.append('Request Information:')
msg_contents.append('='*80)
environ = __request.environ
environkeys = sorted(environ.keys())
for key in environkeys:
msg_contents.append('%s: %s' % (key, environ.get(key)))

msg.body = '\n'.join(msg_contents) + '\n'

mail.send(msg)
import flamejam.views.account
return flamejam.views.account.error(exception)

app.register_error_handler(500, email_exception)

__all__ = ['mail_on_500']
2 changes: 1 addition & 1 deletion flamejam/views/account.py
Expand Up @@ -273,9 +273,9 @@ def settings():

return render_template('account/settings.html', form = form)

#@app.errorhandler(500)
@app.errorhandler(404)
@app.errorhandler(403)
@app.errorhandler(500)
@app.errorhandler(PermissionDenied)
def error(error):
code = error.code if hasattr(error, "code") else 403
Expand Down
2 changes: 1 addition & 1 deletion runserver.py
@@ -1,3 +1,3 @@
from flamejam import app

app.run(debug=True, host="0.0.0.0")
app.run(host="0.0.0.0")

0 comments on commit 0bd0e36

Please sign in to comment.