ExceptionHandler

Exception handling.

class ExceptionHandler.ExceptionHandler(application, transaction, excInfo, formatOptions=None)

Bases: object

Exception handling.

ExceptionHandler is a utility class for Application that is created to handle a particular exception. The object is a one-shot deal. After handling an exception, it should be removed.

At some point, the exception handler sends writeExceptionReport to the transaction (if present), which in turn sends it to the other transactional objects (application, request, response, etc.) The handler is the single argument for this message.

Classes may find it useful to do things like this:

exceptionReportAttrs = ['foo', 'bar', 'baz']
def writeExceptionReport(self, handler):
    handler.writeTitle(self.__class__.__name__)
    handler.writeAttrs(self, self.exceptionReportAttrs)

The handler write methods that may be useful are:

  • write

  • writeTitle

  • writeDict

  • writeAttrs

Derived classes must not assume that the error occurred in a transaction. self._tra may be None for exceptions outside of transactions.

HOW TO CREATE A CUSTOM EXCEPTION HANDLER

In the __init__.py of your context:

from ExceptionHandler import ExceptionHandler as _ExceptionHandler

class ExceptionHandler(_ExceptionHandler):

    _hideValuesForFields = _ExceptionHandler._hideValuesForFields + [
        'foo', 'bar']

    def work(self):
        _ExceptionHandler.work(self)
        # do whatever
        # override other methods if you like

def contextInitialize(app, ctxPath):
    app._exceptionHandlerClass = ExceptionHandler

You can also control the errors with settings in Application.config.

__init__(application, transaction, excInfo, formatOptions=None)

Create an exception handler instance.

ExceptionHandler instances are created anew for each exception. Instantiating ExceptionHandler completes the process – the caller need not do anything else.

basicServletName()

The base name for the servlet (sans directory).

emailException(htmlErrMsg)

Email the exception.

Send the exception via mail, either as an attachment, or as the body of the mail.

errorPageFilename()

Create filename for error page.

Construct a filename for an HTML error page, not including the ErrorMessagesDir setting (which saveError adds on).

filterDictValue(value, key, _dict)

Filter dictionary values.

Filters keys from a dict. Currently ignores the dictionary, and just filters based on the key.

filterValue(value, key)

Filter values.

This is the core filter method that is used in all filtering. By default, it simply returns self._hiddenString if the key is in self._hideValuesForField (case insensitive). Subclasses could override for more elaborate filtering techniques.

htmlDebugInfo()

Return the debug info.

Return HTML-formatted debugging information on the current exception. Calls writeHTML, which uses self.write(...) to add content.

logExceptionToConsole(stderr=None)

Log an exception.

Logs the time, servlet name and traceback to the console (typically stderr). This usually results in the information appearing in console/terminal from which the Application was launched.

logExceptionToDisk(errorMsgFilename=None)

Log the exception to disk.

Writes a tuple containing (date-time, filename, pathname, exception-name, exception-data, error report filename) to the errors file (typically ‘Errors.csv’) in CSV format. Invoked by handleException.

privateErrorPage()

Return a private error page.

Returns an HTML page intended for the developer with useful information such as the traceback.

Most of the contents are generated in htmlDebugInfo.

publicErrorPage()

Return a public error page.

Returns a brief error page telling the user that an error has occurred. Body of the message comes from UserErrorMessage setting.

repr(value)

Get HTML encoded representation.

Returns the repr() of value already HTML encoded. As a special case, dictionaries are nicely formatted in table.

This is a utility method for writeAttrs.

saveErrorPage(html)

Save the error page.

Saves the given HTML error page for later viewing by the developer, and returns the filename used.

servletPathname()

The full filesystem path for the servlet.

setting(name)

Settings are inherited from Application.

work()

Main error handling method.

Invoked by __init__ to do the main work. This calls logExceptionToConsole, then checks settings to see if it should call saveErrorPage (to save the error to disk) and emailException.

It also sends gives a page from privateErrorPage or publicErrorPage (which one based on ShowDebugInfoOnErrors).

write(s)

Output s to the body.

writeAttrs(obj, attrNames)

Output object attributes.

Writes the attributes of the object as given by attrNames. Tries obj._name first, followed by obj.name(). Is resilient regarding exceptions so as not to spoil the exception report.

writeDict(d, heading=None, encoded=None)

Output a table-formatted dictionary.

writeEnvironment()

Output environment.

Writes the environment this is being run in. This is not the environment that was passed in with the request (holding the CGI information) – it’s just the information from the environment that the Application is being executed in.

writeFancyTraceback()

Output a fancy traceback, using CGITraceback.

writeHTML()

Write the traceback.

Writes all the parts of the traceback, invoking:
  • writeTraceback

  • writeMiscInfo

  • writeTransaction

  • writeEnvironment

  • writeIds

  • writeFancyTraceback

writeIds()

Output OS identification.

Prints some values from the OS (like processor ID).

writeMiscInfo()

Output misc info.

Write a couple little pieces of information about the environment.

writeTitle(s)

Output the sub-heading to define a section.

writeTraceback()

Output the traceback.

Writes the traceback, with most of the work done by WebUtils.HTMLForException.htmlForException.

writeTransaction()

Output transaction.

Lets the transaction talk about itself, using Transaction.writeExceptionReport.

writeln(s)

Output s plus a newline.

class ExceptionHandler.Singleton

Bases: object

A singleton object.

ExceptionHandler.docType()

Return the document type for the page.

ExceptionHandler.htStyle()

Return the page style.

ExceptionHandler.htTitle(name)

Format a name as a section title.

ExceptionHandler.osIdDict()

Get all OS id information.

Returns a dictionary containing id information such as pid and uid.