[Welcome] [TitleIndex] [WordIndex

Problem

Quixote's default exception-handling page displays an unadorned paragraph of English text. You may want to provide an internationalized page, or use your site's default template on the exception page.

Solution

Override the Publisher.format_publish_exception() method:

from quixote.errors import AccessError, TraversalError, PublishError, \
         format_publish_error as quixote_format_publish_error

class MyPublisher(Publisher):
    ...
    format_publish_error(self, exc):
        return my_format_publish_error(exc)

def my_format_publish_error(exc):
    if isinstance(exc, NotLoggedInError):
        from mysite.login import redirect_login_page
        return redirect_login_page()
    elif isinstance(exc, AccessError):
        return my_format_access_error(exc)
    elif isinstance(exc, TraversalError):
        return my_format_traversal_error(exc)
    else:
        return quixote_format_publish_error(exc)

def my_format_access_error [html] (exc):
    standard.header("Oops!")
    "<p>We are sorry, but ..."

    # Do something with the exception object 'exc'
    standard.footer()

Discussion

When an exception that is a subclass of PublishError is raised, Quixote stops it's regular publishing procedure, creates a new response object and returns the page returned by Publisher.format_publish_error(). This only applies to PublishError exceptions, which can be raised by a Quixote application even if the application is error-free. Arbitrary Python exceptions that aren't caught are passed to the finish_failed_request() method of the Publisher class. This method logs the exception in the error log and presents a generic page. To change this page, override the _generate_internal_error method in your own custom subclass of Publisher or SessionPublisher.


CategoryCookbook


2010-09-22 22:14