Catch-App Engine for Python中的所有全局异常处理程序

时间:2010-11-28 11:27:01

标签: google-app-engine exception-handling catch-all

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

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

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

3 个答案:

答案 0 :(得分:11)

是的,这是可能的 您可以使用ereporter包来执行此操作,该包允许通过电子邮件从您的应用程序接收异常报告。

Ereporter将报告两种例外情况:

  • 使用logging.exception('Your handled exception')
  • 记录的例外情况
  • 任何未捕获的例外

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

以下是BaseHandler类的一个简单示例:

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', {}))

答案 1 :(得分:1)

您可能希望通过在BaseHandler中调用以下内容来调用原始的handle_exception:

webapp.RequestHandler.handle_exception(self, exception, debug_mode)

这是在上下文中。

from google.appengine.ext import webapp
import sys
import traceback

class BaseHandler(webapp.RequestHandler):
    def handle_exception(self, exception, debug_mode):
        from main import emaildevs
        emaildevs('An error occurred on example.com', ''.join(traceback.format_exception(*sys.exc_info())))
        webapp.RequestHandler.handle_exception(self, exception, debug_mode)

答案 2 :(得分:-3)

尝试:   呼叫 除了:   sendemail

http://docs.python.org/tutorial/errors.html

相关问题