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