Have Pylons abort function send exception emails on errors
I want to be able to use the abort function from pylons.controllers.util to display 404, 500 error pages with info on what went wrong, but at the same time make the abort function send me exception emails from the errormiddleware.
Right now, I've got this:
try:
do(this)
except:
abort(500, 'Something went wrong!')
This disp开发者_StackOverflow社区lays a page with the 500 header and my message, which is nice. But what I want, is to get the standard exception e-mails sent to me, even though I've returned the 500 error myself.
Is there a way to do this? Can I somehow run the errormiddleware function from there? Any ideas appreciated.
Pylons already has very good error reporting to an email via error_email_from and email_to params in the config .ini file.
For example:
[DEFAULT]
email_to = errore@mysite.com
error_email_from = error_robot@mysite.com
Update: There are 2 ways of doing what you want to achieve:
- Define your own error mapper (the right way). Look at http://wiki.pylonshq.com/display/pylonsdocs/Error+Documents for code sample.
- Send out an email in
error.py
controller's 'document
' method (fast but hacky way).
in your middleware.py you should have this:
if asbool(full_stack):
# Handle Python exceptions
app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
# Display error documents for 401, 403, 404 status codes (and
# 500 when debug is disabled)
if asbool(config['debug']):
app = StatusCodeRedirect(app)
else:
app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
it handles your errors. but if you want to send email when error occurs you can write helper function that you call in the critical section of your code which will send details of the error to your email or you can rewrite this middleware (ErrorHandler).
ofc you can also just add that helper function call in existing ErrorHandler but i don't recommend it (it's not good programming to modify existing lib).
the code of that helper function:
import turbomail
def send_mail(body, author,subject, to):
conf = {
'mail.on': True,
'mail.transport': 'smtp',
'mail.smtp.server': 'smtp.DOMAIN.SMT:25',
}
turbomail.interface.start(conf)
message = turbomail.Message(
author = author,
to = to,
subject = subject,
plain = body,
encoding = "utf-8"
)
message.send()
turbomail.interface.stop()
hope it helps...
How about just causing an exception by hand, i.e. raise(SomeMeaningfulException)? That should get handled automatically by the errormiddleware.
精彩评论