1.6.4 Example

Take a look at the example below:

#!/usr/bin/env python

# show python where the web modules are
import sys
sys.path.append('../')
sys.path.append('../../../') 

def handler(dict, params={}):
    p = {
        'email':None,
        'message':"""<html>
                       <head><title>An Error Occured</title></head>
                       <body><h1>An Error Occured</h1>
                       <p>The devlopers have been informed</p></body>
                    </html>""",
        'reply':'Website Error <none@example.com>'
    }
    for k,v in params.items():
        p[k] = v
    
    if not p.has_key('email'):
        raise Exception('You must specify the email parameter.')
        
    import web, web.mail
    web.mail.send(
        msg="There was an exception in the code of the website.\n\n"+dict['text'],
        to=p['email'],
        reply=p['reply'],
        subject='Error in website',
        sendmail='usr/bin/sendmail',
        smtp='smtp.ntlworld.com',
        method='smtp',# could use method='sendmail' to send using sendmail.
    )
    print web.header()
    print p['message']

import web.error
web.error.handle(
    handler, 
    email = 'james@example.com',
    message = """<html>
           <head><title>An Error Occured</title></head>
           <body><h1>Internal Error</h1>
           <p>The devlopers have been informed.</p></body>
        </html>""",
    reply = 'Developer <devloper@example.com>'
)

raise Exception('This is a test exception')

Warning: If you run this example please make sure you replace the email addresses with your own email address in. You may need to change the path of sendmail or use an SMTP server instead. See the web.mail module documentation for help with this.

Note: If an exception occurs in your custom error handling function it may be difficult to track down. You can put your code inside a try except block and make sure some sensible output is returned in the event of an Exception being raised.

See About this document... for information on suggesting changes.