是否可以在使用Python的Google App Engine中創建全部異常處理程序?適用於Python的App Engine中的Catch-All全局異常處理程序
基本上,我想捕獲所有未捕獲的異常並優雅地處理它,同時向我發送帶回溯的電子郵件。
目前,對於所有未被捕獲的錯誤,用戶會看到一個帶有代碼片段的堆棧跟蹤。這是不可取的。
是否可以在使用Python的Google App Engine中創建全部異常處理程序?適用於Python的App Engine中的Catch-All全局異常處理程序
基本上,我想捕獲所有未捕獲的異常並優雅地處理它,同時向我發送帶回溯的電子郵件。
目前,對於所有未被捕獲的錯誤,用戶會看到一個帶有代碼片段的堆棧跟蹤。這是不可取的。
是的,它是可能的。
您可以使用ereporter軟件包來完成此操作,該軟件包允許通過電子郵件從應用程序接收異常報告。
Ereporter將報告兩種例外:
logging.exception('Your handled exception')
要捕獲所有異常,我想創建一個自定義BaseHandler類中重寫handle_exception()方法;所有的請求處理程序都應該從這個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', {}))
您可能希望通過調用你的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)
的應用程序中捕獲全局錯誤!太棒了!這似乎解決了我的一半問題,但啓用此功能時,用戶會看到何時發生未捕獲的異常?有沒有辦法將用戶重定向到所有未處理的異常的默認錯誤頁面? – Derick 2010-11-28 12:21:35