Catch-App Engine for Python中的所有全局异常处理程序 [英] Catch-All global exception handler in App Engine for Python

查看:190
本文介绍了Catch-App Engine for Python中的所有全局异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Python在Google App Engine中创建全局全局异常处理程序?

Is it possible to create a catch-all global exception handler in Google App Engine using Python?

基本上,我想捕获所有未捕获的异常并且优雅处理它,同时向我发送带有追溯的电子邮件。

Basically, I want to catch all un-caught exceptions and gracefully handle it, while sending an email with the traceback to me.

目前,对于所有未捕获的错误,用户看到一个堆栈跟踪,其中包含一段代码。这是不可取的。

Currently, for all uncaught errors, the users see a stacktrace with a snippet of code in it. This is undesirable.

推荐答案

是的,可以。

你可以使用 ereporter 包,允许通过电子邮件从您的应用程序接收异常报告。

Yes it is possible.
You can do it using the ereporter package that allows to receive exception reports from your application by email.

Ereporter将报告两种异常:

Ereporter will report two kind of exceptions:


  • 使用 logging.exception('你处理的异常')

  • 任何未捕获的例外

要捕获所有异常,我将创建一个自定义的BaseHandler类,覆盖 handle_exception()方法;所有您的请求处理程序都应该从此Base类继承。

查看自定义错误响应

To catch all the exceptions, I would create a custom BaseHandler class overriding the handle_exception() method; all your request handlers should inherit from this Base class.
Have a look to Custom Error Responses too.

这是一个简单的BaseHandler类的例子:

Here is a simple example of BaseHandler class:

class BaseHandler(webapp.RequestHandler):

    def handle_exception(self, exception, debug_mode):
        if debug_mode:
            webapp.RequestHandler.handle_exception(self, exception, debug_mode)
        else:
            logging.exception(exception)
            self.error(500)
            self.response.out.write(template.render('templdir/error.html', {}))

这篇关于Catch-App Engine for Python中的所有全局异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆