Hi,
I needed not just use a custom error_page_file, or HTTPError message,
but to be able to send a completely custom error. I made a previous
post on cherrypy-users about it here:
http://groups.google.com/group/cherrypy-users/browse_frm/thread/2a75748e8eb0463e
To solve this I've modified cherrypy._cperror.py So that you can do
something like the following:
def foobar(uri):
try:
location = get_uri(uri)
data = get_data(location)
success = Template(file=filename, searchList=[data])
return success.respond()
except:
error = Template(file=filename, searchList=[locals()])
raise HTTPError(404, body=error)
foobar.exposed = True
The modifications to _cperror.py are listed below:
The complete _cperror.py file is here:
http://groups.google.com/group/cherrypy-devel/web/_cperror.py
Please consider this to be included. :)
@class HTTPError(CherryPyException):
- def __init__(self, status=500, message=None):
+ def __init__(self, status=500, message=None, body=None):
+ self.body = body
@class HTTPError(CherryPyException):
def set_response(self):
- content = get_error_page(self.status, traceback=tb,
message=self.message)
+ content = get_error_page(self.status, traceback=tb,
message=self.message, body=self.body)
@def get_error_page(status, **kwargs):
- # Replace the default template with a custom one?
- error_page_file = cherrypy.request.error_page.get(code, '')
-
- if error_page_file:
- try:
- template = file(error_page_file, 'rb').read()
- except:
- m = kwargs['message']
- if m:
- m += "<br />"
- m += ("In addition, the custom error page "
- "failed:\n<br />%s" % (_exc_info()[1]))
- kwargs['message'] = m
-
- return template % kwargs
+ if kwargs["body"]:
+ template = kwargs["body"]
+ return template
+ else:
+ # Replace the default template with a custom one?
+ error_page_file = cherrypy.request.error_page.get(code, '')
+
+ if error_page_file:
+ try:
+ template = file(error_page_file, 'rb').read()
+ except:
+ m = kwargs['message']
+ if m:
+ m += "<br />"
+ m += ("In addition, the custom error page "
+ "failed:\n<br />%s" % (_exc_info()[1]))
+ kwargs['message'] = m
+
+ return template % kwargs