1

I have an issue with the CherryPy usage and all investigation has not lead to a working solution so far.

I would like to handle and display in a custom page all server errors (Errors with status 500). For example if there is a specific database error, I'd like to display it to the user and give also the string of the Exception.

To generate the error page I am currently using:

_cp_config = {'error_page.500': '<path_to_html_file>'}

But I cannot find out how to catch and pass the exception string and eventually add it to the output to the user.

Note that there are couple of services for communication between the front end and the back end so I'd like to avoid the "try except" structure.

Any advice on how to do it will be useful.

Thanks a lot.

Regards, Teddy

Teddy Markov
  • 266
  • 3
  • 15
  • Can you elaborate a bit more as to why "try-except" is not an option? Because it will do exactly what you ask, and there may be a way around your limitations. – Hal T Jan 27 '17 at 15:30
  • I am not quite familiar with the CherryPy but after turning to the service with "raise cherrypy.HTTPRedirect('') currently the content is rendered by the service and returned using "return template.render()". I haven't found a way to try-except this way of communication having in mind that when the rendered content is sent, javascript keeps communicating with other back end services. – Teddy Markov Jan 27 '17 at 15:49
  • 1
    Does [this answer](http://stackoverflow.com/a/20406691/5086961) solve your issue? – Hal T Jan 27 '17 at 15:57
  • It answers it partially. I am looking for a way to display the exception too. – Teddy Markov Jan 29 '17 at 11:46

1 Answers1

4

The config options for the error handling can be configured to accept a callable with the following signature: (status, message='', traceback='', version='')

You could define:

def error_404(status, message='', traceback='', version=''):
    # do something, like rendering a template or send an email
    return "Not Found" # or return your rendered template

def error_500(status, message='', traceback='', version=''):
    # do something, like rendering a template or send an email
    return "Error 500" # or return your rendered template

_cp_config = {
   'error_page.500': error_500,
   'error_page.404': error_404
 }
cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
  • I liked this method - it fits perfectly but how can I put the message from the exception - for example if there is a database outage? – Teddy Markov Jan 28 '17 at 22:11
  • 1
    If the traceback is not passed as an argument (the config `request.show_traceback` is `False`), then you can get the traceback with: `import cherrypy._cperror as cperror` and then get the last traceback with `cperror.format_exc()`. Or if you only want the function, `from cherrypy._cperror import format_exc`. The function is based on the [stdlib traceback module](https://docs.python.org/3.6/library/traceback.html). – cyraxjoe Jan 29 '17 at 21:46
  • 1
    For _completeness_ you can override all the error responses by configuring `request.error_response` but you I'll have to do some extra work, the function doesn't take any arguments, I recommend you to override `error_page.500` instead. – cyraxjoe Jan 29 '17 at 21:50
  • 1
    This helped and with combination to the answer above gives the solution to the problem. I found that for a clearer output `sys.exc_info()[*]` can also be used in some situations. – Teddy Markov Jan 30 '17 at 15:56