开发者

How can I setup a global DeadlineExceededError handler?

开发者 https://www.devze.com 2023-03-23 16:01 出处:网络
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'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 global开发者_开发技巧ly catch these elusive suckers?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号