I'm having an issue providing custom response handling for cherrypy.HTTPError
. The only content that I want displayed in the body of the response is a JSON-encoded dict (this is REST API). The source code seems to indicate that HTTPError.set_response()
can be used to modify the cherrypy.response
object ... to quote the comment in this method:
Modify cherrypy.response status, headers, and body to represent self. CherryPy uses this internally, but you can also use it to create an HTTPError object and set its output without *raising* the exception.
I have sub-classed HTTPError to provide my own body for the response. I call the base class methods to ensure that any necessary housekeeping takes place.
class APIError(cherrypy.HTTPError):
def __init__(self, err_resp):
super().__init__(status=err_resp['error_code'])
self._api_err_resp = err_resp
def set_response(self):
super().set_response()
response = cherrypy.serving.response
response.body = json.dumps(self._api_err_resp).encode()
I can now call APIError
without a problem, but the issue I have is that the CherryPy web server takes approx 10-15sec to respond to my client once my custom error is raised (I experience no delay if I use HTTPError
). I've traced the source code, but can't find the cause of the delay.
Any help would be appreciated.
Rob