基於一些Google搜索,我安裝了以下錯誤處理程序。然而,似乎返回一個http 500的python異常不會被這個東西困住,雖然404是。通過下面的代碼留下的打印聲明,我可以看到它沒有遇到任何這些例程。我應該怎麼做?在Tornado應用程序中優雅地處理應用程序異常
class ErrorHandler(tornado.web.RequestHandler):
"""Generates an error response with status_code for all requests."""
def __init__ (self, application, request, status_code):
print 'In ErrorHandler init'
tornado.web.RequestHandler.__init__(self, application, request)
self.set_status(status_code)
def get_error_html (self, status_code, **kwargs):
print 'In get_error_html. status_code: ', status_code
if status_code in [403, 404, 500, 503]:
filename = '%d.html' % status_code
print 'rendering filename: ', filename
return self.render_string(filename, title=config.get_title())
return "<html><title>%(code)d: %(message)s</title>" \
"<body class='bodyErrorPage'>%(code)d: %(message)s</body>"\
"</html>" % {
"code": status_code,
"message": httplib.responses[status_code],
}
def prepare (self):
print 'In prepare...'
raise tornado.web.HTTPError(self._status_code)
Iboola,謝謝你的迴應。通過爲hanlders聲明一個write_error()方法,我可以捕獲500個數據;但是,這種方法並不適用於處理整個網站的404! 所以我不得不把我分配給tornado.web.ErrorHandler的一個全局錯誤處理程序類,除了一個基類和你所建議的write_error()方法。現在我能夠捕獲我的應用程序異常,除了全局404s。 – Karra 2012-07-10 05:44:47