1.6.4 Using The error() Function

Alternatively you can create an ErrorInformation object to display the error information:

try:
    raise Exception('This error will be caught and nicely displayed')
except:
    import web.error
    errorInfo = web.error.error()
    print error.textException()

This would aproduce the same output described in the previous example.

The web.error.error() function returns an ErrorInformation object which can be used to format exception tuples in a variety of useful ways. Below is the API reference for the web.error.error() function and the Information objects returned.

web.error.error( [error=sys.exc_info()], [context=5])

Return an ErrorInformation object representing the error.

error
The traceback tuple you wish to display information for. If not specified the last exception is used.

context
The default number of lines of code to display in traceback information. The default is 5.

class ErrorInformation

Error Information objects have the following attributes:

error
The error tuple specified in the info() function or sys.exc_info() if no error was specified.

format
The default output format of the methods. Can currently be 'text' or 'html'.

pythonVersion
A string representing the version of Python being used.

errorType
The Exception raised

errorValue
The error message.

date
A string representing the date and time the Information object was created. Note: This may not be the time the error occured.

context
The number of lines of code to display in error information.

Error Information objects have the following methods for displaying error information Note: Python 2.1 and below do not have the cgitb module and so have slightly different implementations of the html() and text() methods so the output of those methods may be different to the output generated using Python 2.2 and above.

ouput( output, [format], [error], [context])
output can be 'traceback' for a traceback, 'code' for a code listing or 'debug' for code and traceback listing suitable for script debugging. The method returns the result of calling the respective method below.

traceback( [format], [error])
Returns the traceback of the error in the format specified by format which can currently be 'text' or 'html'. If not specified format takes the value of format. error should be an error tuple as returned by sys.exc_info(). If not specified error is used.

code( [format], [error], [context])
Returns relevant lines of code and variables from the traceback in the format specified by format which can currently be 'text' or 'html'. If not specified format takes the value of format. context is the number of lines of code to display at each stage in the traceback information. If not specified context is used. error should be an error tuple as returned by sys.exc_info(). If not specified error is used.

debug( [format], [error], [context])
Returns the traceback of the error in the format specified by format together with relevant lines of code and variables. format can currently be 'text' or 'html'. If not specified format takes the value of format. context is the number of lines of code to display at each stage in the traceback information. If not specified context is used. error should be an error tuple as returned by sys.exc_info(). If not specified error is used.