Custom HTTP Errors

12 views
Skip to first unread message

braydon fuller

unread,
Feb 3, 2008, 9:13:38 PM2/3/08
to cherrypy-devel
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

braydon fuller

unread,
Feb 3, 2008, 9:43:33 PM2/3/08
to cherrypy-devel
Also another modification will be needed to _cperror.py

@get_error_page(status, **kwargs):

- for k, v in kwargs.iteritems():
- if v is None:
- kwargs[k] = ""
- else:
- kwargs[k] = _escape(kwargs[k])

+ for k, v in kwargs.iteritems():
+ if v is None:
+ kwargs[k] = ""
+ else:
+ if k == "body":
+ pass
+ else:
+ kwargs[k] = _escape(kwargs[k])


-Braydon

On Feb 3, 6:13 pm, braydon fuller <cour...@braydon.com> wrote:
> 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/2a757...
Reply all
Reply to author
Forward
0 new messages