title: CherryPy HTTPError custom response handling (*NOT* HTML)
body: <p>I'm having an issue providing custom response handling for <code>cherrypy.HTTPError</code>. 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 <code>HTTPError.set_response()</code> can be used to modify the <code>cherrypy.response</code> object ... to quote the comment in this method:</p>
  
  <p><em>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.</em></p>
  
  <p>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.</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>I can now call <code>APIError</code> 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 <code>HTTPError</code>). I've traced the source code, but can't find the cause of the delay.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Rob</p>