I use CherryPy to run a very simple web server. It is intended to process the GET
parameters and, if they are correct, do something with them.
import cherrypyclass MainServer(object):def index(self, **params):# do things with correct parametersif 'a' in params:print params['a']index.exposed = Truecherrypy.quickstart(MainServer())
For example,
http://127.0.0.1:8080/abcde:404 Not FoundThe path '/abcde' was not found.Traceback (most recent call last):File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 656, in respondresponse.body = self.handler()File "C:\Python27\lib\site-packages\cherrypy\lib\encoding.py", line 188, in __call__self.body = self.oldhandler(*args, **kwargs)File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 386, in __call__raise self
NotFound: (404, "The path '/abcde' was not found.")
Powered by CherryPy 3.2.4
I am trying to catch this exception and show a blank page because the clients do not care about it. Specifically, the result would be an empty body, no matter the url or query string that resulted in an exception.
I had a look at documentation on error handling cherrypy._cperror
, but I did not find a way to actually use it.
Note: I gave up using CherryPy
and found a simple solution using BaseHTTPServer
(see my answer below)
Docs somehow seem to miss this section. This is what I found while looking for detailed explanation for custom error handling from the source code.
Custom Error Handling
Anticipated HTTP responses
The 'error_page' config namespace can be used to provide custom HTML output for
expected responses (like 404 Not Found). Supply a filename from which the
output will be read. The contents will be interpolated with the values
%(status)s, %(message)s, %(traceback)s, and %(version)s using plain old Python
string formatting.
_cp_config = {'error_page.404': os.path.join(localDir, "static/index.html")
}
Beginning in version 3.1, you may also provide a function or other callable as
an error_page entry. It will be passed the same status, message, traceback and
version arguments that are interpolated into templates
def error_page_402(status, message, traceback, version):return "Error %s - Well, I'm very sorry but you haven't paid!" % status
cherrypy.config.update({'error_page.402': error_page_402})
Also in 3.1, in addition to the numbered error codes, you may also supply
error_page.default
to handle all codes which do not have their own error_page
entry.
Unanticipated errors
CherryPy also has a generic error handling mechanism: whenever an unanticipated
error occurs in your code, it will call
Request.error_response
to
set the response status, headers, and body. By default, this is the same
output as
HTTPError(500)
. If you want to provide
some other behavior, you generally replace "request.error_response".
Here is some sample code that shows how to display a custom error message and
send an e-mail containing the error
from cherrypy import _cperrordef handle_error():cherrypy.response.status = 500cherrypy.response.body = ["<html><body>Sorry, an error occurred</body></html>"]sendMail('[email protected]','Error in your web app',_cperror.format_exc())@cherrypy.config(**{'request.error_response': handle_error})
class Root:pass
Note that you have to explicitly set
response.body
and not simply return an error message as a result.