Error HandlingΒΆ

Just an example for now.

from zope.interface import implements

from twisted.application import service, strports
from twisted.web import http

from nevow import appserver, context, inevow, loaders, rend, static, tags as T, url

# Inspired by

class ErrorPage(rend.Page):
    def render_lastLink(self, ctx, data):
        referer = inevow.IRequest(ctx).getHeader('referer')
        if referer:
            return T.p[T.a(href=referer)['This link'],
                       ' will return you to the last page you were on.']
            return T.p['You seem to be hopelessly lost.']

class The500Page(ErrorPage):
    docFactory = loaders.xmlstr('''
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
<html xmlns:n=''>
        <title>500 error</title>
        <style type='text/css'>
        body { border: 6px solid red; padding: 1em; }
        <h1>Ouchie. Server error.</h1>
        <p>The server is down.</p>
        <p><em>Ohhh. The server.</em></p>
        <p n:render='lastLink' />

    def renderHTTP_exception(self, ctx, failure):
        request = inevow.IRequest(ctx)
        res = self.renderHTTP(ctx)
        request.finishRequest( False )
        return res

class The404Page(ErrorPage):
    docFactory = loaders.xmlstr('''
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
<html xmlns:n=''>
        <title>404'd !!!</title>
        <style type='text/css'>
        body { border: 6px solid orange; padding: 1em;}
        <h1>OW! my browser!</h1>
        <p>Were you just making up names of files or what?</p>
        <p>(apologies to</p>
        <p n:render='lastLink' />
    def renderHTTP_notFound(self, ctx):
        return self.renderHTTP(ctx)

class OopsPage(rend.Page):
    def exception(self, ctx, data):
        1/0 # cause an exception on purpose
    docFactory = loaders.stan(exception)

class RootPage(rend.Page):
    docFactory = loaders.stan(
            T.html[T.head[T.title['Error Pages']],
                   T.body[T.h1['The Only Real Page'],
                            'This link goes nowhere, pleasantly.'],
                            'This link causes an exception!'],


    def locateChild(self, ctx, segments):
        ctx.remember(The404Page(), inevow.ICanHandleNotFound)
        ctx.remember(The500Page(), inevow.ICanHandleException)
        return rend.Page.locateChild(self, ctx, segments)

root = RootPage()
root.putChild('oops', OopsPage())

site = appserver.NevowSite(root)
# You should be able to do this instead of in locateChild, but there seems to be a bug
#site.remember(The404Page(), inevow.ICanHandleNotFound)
#site.remember(The500Page(), inevow.ICanHandleException)

application = service.Application('News item editor')
strports.service('8080', site).setServiceParent(application)