Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Send mail on error with stacktrace

  • Loading branch information...
commit 0bd0e36942b087b2e186aa816dc124ff034cf18b 1 parent 531ca55
@svenstaro authored
View
3  flamejam/__init__.py
@@ -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__)
@@ -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'))
View
68 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']
View
2  flamejam/views/account.py
@@ -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
View
2  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")
Please sign in to comment.
Something went wrong with that request. Please try again.