Google App Engine中的全球异常处理 [英] Global Exception Handling in Google App Engine

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

问题描述

而不是将整个代码封装在除{}块之外的try {}中,是否在全球范围内捕获异常?

Instead of encapsulating my entire code in a try{} except{} block, is there someway of catching exceptions globally?

基本上我正在寻找一种方法有一个全局异常处理程序,它将处理我为应用程序引擎编写的python应用程序中的所有未处理的异常。

Basically I am looking for a way to have a global exception handler which will handle all unhandled exceptions in the my python application written for google app engine

推荐答案

使用webapp框架,您应该已经定义了一个用作基类的RequestHandler子类,所有应用程序的处理程序都将扩展。您可以简单地覆盖 handle_exception 作为任何未捕获的异常的全局异常处理程序。

If you're using the webapp framework, you should already be defining a subclass of RequestHandler that serves as a base class, with all your app's handlers extending that. You can simply override handle_exception, which serves as a global exception handler for any uncaught exceptions.

默认实现调用self.error(500),记录异常,如果调试打开,则输出一个stacktrace。

The default implementation calls self.error(500), logs the exception, and if debug is on, outputs a stacktrace.

如果您使用另一个框架,您可以编写一块调用包装的WSGI应用程序的WSGI中间件,并捕获任何抛出的异常,将它们作为你希望。

If you're using another framework, you could write a piece of WSGI middleware that calls the wrapped WSGI app, and catches any thrown exceptions, dealing with them as you wish.

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

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