Custom error pages

49 views
Skip to first unread message

qvx

unread,
Apr 20, 2006, 10:46:11 AM4/20/06
to TurboGears
After looking for some way to implement custom 404 page, I came upon
several pages on the subject but none of them provided a complete
solution.

This is what I came up with (after googling).
It handles 404 and other errors like 500 ...

class Root(controllers.RootController):
...
def _cp_on_http_error(self, status, message):

log.exception('%s %s', status, message)

# Make sure response is properly prepared

from cherrypy import _cputil
_cputil._cp_on_http_error(status, message)

# Customize returned page

title = {
400: u'Loš zahtjev',
401: u'Neautorizirani pristup',
403: u'Zabranjeni pristup',
404: u'Program ne može pronaci traženi resurs',
500: u'Interna pogreška',
501: u'Nije implementirano',
502: u'Servis je privremeno opterecen',
}.get(status, message or u'Greška')

import traceback, StringIO
detailsio = StringIO.StringIO()
traceback.print_exc(file = detailsio)

output = dict(
status = status,
message = message or '-',
title = title,
admin = 'admin' in identity.current.identity().groups,
url = "%s %s" % (cherrypy.request.method,
cherrypy.request.path),
details = detailsio.getvalue())

template = "ccleads.templates._error"
format = 'html'
content_type = 'text/html'
mapping = None

# Return customized page
body = controllers._process_output(output, template, format,
content_type, mapping)
cherrypy.response.headers['Content-Length'] = len(body)
cherrypy.response.body = body


Can somebody see any flaws in this?

Thanks,
Tvrtko

ajones

unread,
Apr 20, 2006, 11:23:40 AM4/20/06
to TurboGears
Why are you logging 404 errors? I would assume that 98% of the entries
for that would be someone misspelling a url, so why not wait until you
have the return status and log everything else?

qvx

unread,
Apr 20, 2006, 12:32:23 PM4/20/06
to TurboGears
I guess I could skip logging 404 errors. But I'm gonna wait a week or
two because I just restructured my app in a major way. I wish I had
Routes support.

Reply all
Reply to author
Forward
0 new messages