1.6.5 Creating Custom Handlers

If the built-in handlers don't provide the level of cutomisation you require you can create a custom handler.

Handlers are simply callables which take the info string to output as the first parameter and any parameters passed to the handle() function as subsequent parameters.

For example:

>>> def myHandler(info, message):
...     print message
>>>
>>> import web.error; web.error.handle(myHandler, message="An error occured")
>>> raise Exception('This is an error')
An error occured

This example isn't too useful as it always displays the same output. To make it more useful

>>> def myHandler(info, message):
...     print message
...     print info
>>>
>>> import web.error
>>> web.error.handle(
...     myHandler, 
...     format='text', 
...     output='traceback', 
...     message='An error occured',
... )
>>> raise Exception('This is an error')
An error occured
exceptions.Exception: This is an error
    args = ('This is an error',)

output is used to obtain the error information from the info() function which is then sent as the first parameter to the myHandler function. message is also sent to the myHandler function which prints the error message.

This structure allows building very powerful handlers.