web2py users, web2pys default mechanism for exception handling works great in development but when shifted into production I'd rather have the application email on exception and display a sorry message to the user. I'm proposing a new file in the root of web2py "customexception.py", If this file does not exist (or if it returns None or False) web2py's default exception mechanism is used. If it does exist and returns a str then the code within is executed.
I've coded this up quickly in the last hour or so. I pass the following environment to be executed in "customexception.py" environment={'layer':str(self.layer), 'code':str(self.code), 'output':str(self.output), 'traceback':str(self.traceback), 'application': request.application, 'rv': None} where layer => path to executing code code => block of code that exec was called on output => string output, not sure how to use this, maybe rv is redundant? traceback => exception traceback application => application being executed rv => return value for this handler, set to None or False to do nothing set to string for text to display to user within your file you set rv to a string to display to the user, set it to None or False to have web2pys default mechanism used. An example of a "customexception.py" file would be: [code] ''' Author: Mark Larsen Date: 11/12/08 Method to handle custom exceptions If this file exists and rv evaluates true web2py will use this to handle exceptions instead of built-in method I mainly want this to send me emails in a production environment. variables passed in are layer => path to executing code code => block of code that exec was called on output => string output traceback => exception traceback application => application being executed rv => return value for this handler, set to None or False to do nothing set to string for text to display to user ''' import smtplib smtpserver = 'localhost' ## assumes smtp server on localhost, i use sendmail toaddrs = ['[EMAIL PROTECTED]'] fromaddr = '[EMAIL PROTECTED]' subject = 'Unhandled Exception in %s application' % application msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs), subject)) msg+="An error has occured in %s \n\n" % layer msg+=traceback msg+="\n\nIn code block: \n\n" msg+=code server = smtplib.SMTP(smtpserver) server.sendmail(fromaddr,toaddrs,msg) server.quit() rv = "Unfortunately a serious error has occurred." [/code] Anyone interested in something like this? Thanks, Mark --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---