Global Exception Handling in Google App Engine

﹥>﹥吖頭↗ 提交于 2019-11-30 20:05:53

问题


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

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


回答1:


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.

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

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.




回答2:


Well, at the most basic level you could wrap all of your handler scripts referenced by app.yaml in a giant try-except block.

If you are using the webapp framework, consider overriding handle_exception() for each of your request handlers. If you want all of your request handlers to have some basic exception handling that you specify, you could a request handler which implements this method and then derive all of your handlers from it.




回答3:


You application probably has a main() function, put the try/except in that function, and it'll catch everything from your application.



来源:https://stackoverflow.com/questions/3154900/global-exception-handling-in-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!