>From some very old code (may or may not work anymore), demonstrating
using _cpOnError to catch an exception an stack trace and log it.
Replace the log() instruction with something that emails errorBody and
you should be good. Note the distinction between production and dev
environments to make sure you still get the displayed stack trace etc
for dev work.
Note this is a different approach from using the exc_handler, which is
designed more for handling specific exceptions in a particular method
or group, rather than general catching of unexpected exceptions for
the purposes of alerting devs to broken things.
class Root(controllers.RootController):
def _cpOnError(self):
""" Manages exceptions and produces better debugging
information """
if config.get('server.environment') == 'production':
try:
raise
except NotFound, e:
raise e
except Exception, e:
import traceback, StringIO
bodyFile = StringIO.StringIO()
traceback.print_exc(file = bodyFile)
errorBody = bodyFile.getvalue()
log("ERROR occurred: %s" % errorBody)
flash("An unexpected application error occured")
raise redirect('/error')
else:
import traceback, StringIO
bodyFile = StringIO.StringIO()
traceback.print_exc(file = bodyFile)
errorBody = bodyFile.getvalue()
log("ERROR occurred: %s" % errorBody)
cherrypy.response.body = ['<pre>%s</pre>' %
str(errorBody)]
cherrypy.response.headerMap['Status'] = '500 Unexpected
Application Error'
On Mar 13, 5:01 am, Marco Mariani <[EMAIL PROTECTED]> wrote:
> I'm trying to mail myself the stack trace, should an exception occur in
> production servers, like I'm used to do with other programs.
>
> Followinghttp://docs.turbogears.org/1.0/ErrorHandling, I set up a test
> controller:
>
> > class Root(controllers.RootController):
> > def exc_handler(self, tg_exceptions=None):
> > tb = traceback.format_exception(*sys.exc_info())
> > import pdb; pdb.set_trace()
> > return dict(handling_value=True, exceptions=str(tg_exceptions))
>
> > @expose()
> > @exception_handler(exc_handler)
> > def exceptional(self):
> > raise ValueError("BOOOH!")
>
> At the breakpoint, the exception I see is
>
> (Pdb) tb
> ['Traceback (most recent call last):\n', ' File "_speedups.pyx", line
> 278, in _speedups.__getitem__\n', 'KeyError: 8\n']
>
> I guess at this time the original exception is lost, isn't it?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---