8

I'd like to catch and handle DeadlineExceededError so users don't see the standard "Server Error" page that App Engine throws by default.

I know that DeadlineExceededErrors are not caught when overriding handle_exception in your request handler (we already do this).

I have tried, unsuccessfully so far, to use the custom error_handlers app.yaml configuration like so:

error_handlers:
  - error_code: timeout
    file: timeout.html

...but that also doesn't seem to catch DeadlineExceededErrors, unless I'm doing something wrong.

I am aware that I can use the following pattern to catch DeadlineExceededErrors inside particular request handlers:

class MainPage(webapp.RequestHandler):
    def get(self):
        try:
            # Do stuff...
        except DeadlineExceededError:
            # Many Whelps! Handle it!

...but I would like to avoid adding this to every single request handler in my application.

How can I globally catch these elusive suckers?

kamens
  • 11,910
  • 6
  • 45
  • 46

1 Answers1

3

One possible solution is to use webapp2, which is a pretty neat framework as it is and has a lot of useful stuff over the original webapp. With webapp2, you can handle the exception in the handle_500 method, as follows:

def BaseHandler(webapp2.RequestHandler):
    def handle_500(request, response, exception):
        if isinstance(exception, DeadlineExceededError):
            response.write('Deadline exceeded!')
        else:
            response.write('A server error occurred!')

        logging.exception(exception)
        response.set_status(500)
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
waffle paradox
  • 2,755
  • 18
  • 19
  • In webapp2, you could set an error handler to handle 500 status code -- mostly used for uncaught exceptions. There, check what type of exception it is and handle accordingly: if isinstance(exception, DeadlineExceededError): ... Docs are here: http://webapp-improved.appspot.com/guide/exceptions.html#exceptions-in-the-wsgi-app – moraes Jul 28 '11 at 03:01
  • I actually didn't know that; yes, that would be a more appropriate place to put the error handling. – waffle paradox Jul 28 '11 at 03:04
  • 1
    This is what I'd suggest. Please, please don't send redirects for errors, though - serve up the error page on the same URL. – Nick Johnson Jul 28 '11 at 03:08
  • Yes, that was just filler. I do suppose it makes a bad example, I'll change it. – waffle paradox Jul 28 '11 at 03:09
  • @moraes this resembles pretty much what [web.py](http://webpy.org/cookbook/custom_notfound) does, it's very handy. – systempuntoout Jul 28 '11 at 07:20