2012-07-09 111 views
8

基於一些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) 

回答

9

首先,你是在養有prepare代碼200異常,因此它不是在get_error_html功能捕獲。

其次,get_error_html已棄用:使用write_error,而不是(write_error)。

最後,你不需要調用__init__ErrorHandler:初始化函數使用initializeinitialize),但在這種情況下,你不需要它。

這裏是一個工作示例:

import tornado 
import tornado.web 


class ErrorHandler(tornado.web.RequestHandler): 
    """Generates an error response with status_code for all requests.""" 

    def write_error(self, status_code, **kwargs): 
     print 'In get_error_html. status_code: ', status_code 
     if status_code in [403, 404, 500, 503]: 
      self.write('Error %s' % status_code) 
     else: 
      self.write('BOOM!') 

    def prepare(self): 
     print 'In prepare...' 
     raise Exception('Error!') 


application = tornado.web.Application([ 
     (r"/", ErrorHandler), 
     ]) 

if __name__ == "__main__": 
    application.listen(8899) 
    tornado.ioloop.IOLoop.instance().start() 
+0

Iboola,謝謝你的迴應。通過爲hanlders聲明一個write_error()方法,我可以捕獲500個數據;但是,這種方法並不適用於處理整個網站的404! 所以我不得不把我分配給tornado.web.ErrorHandler的一個全局錯誤處理程序類,除了一個基類和你所建議的write_error()方法。現在我能夠捕獲我的應用程序異常,除了全局404s。 – Karra 2012-07-10 05:44:47

7
  1. 處理程序。讓我們定義一些默認處理程序我們會使用
import tornado.web 


class BaseHandler(tornado.web.RequestHandler): 
    """ 
    Base handler gonna to be used instead of RequestHandler 
    """ 
    def write_error(self, status_code, **kwargs): 
     if status_code in [403, 404, 500, 503]: 
      self.write('Error %s' % status_code) 
     else: 
      self.write('BOOM!') 


class ErrorHandler(tornado.web.ErrorHandler, BaseHandler): 
    """ 
    Default handler gonna to be used in case of 404 error 
    """ 
    pass 


class MainHandler(BaseHandler): 
    """ 
    Main handler 
    """ 
    def get(self): 
     self.write('Hello world!') 
  • 設置。我們需要定義default_handler_classdefault_handler_args以及
  • settings = { 
        'default_handler_class': ErrorHandler, 
        'default_handler_args': dict(status_code=404) 
    } 
    
  • 應用。
  • application = tornado.web.Application([ 
        (r"/", MainHandler) 
    ], **settings) 
    

    作爲結果。所有錯誤,除了404將由BaseHandler處理。 404 - ErrorHandler。就是這樣:)

    -1
    import tornado.web 
    
    
    class BaseHandler(tornado.web.RequestHandler): 
        def write_error(self, status_code, **kwargs): 
         print status_code 
         super(BaseHandler, self).write_error(status_code, **kwargs) 
    
    +0

    請添加一些上下文,解釋或任何相關的評論,而不是僅僅是無環境的代碼。 – Sebastialonso 2017-01-16 14:08:38

    相關問題