2012-07-09 12 views
8

कुछ googling के आधार पर मैंने निम्न त्रुटि हैंडलर स्थापित किया। हालांकि पाइथन अपवाद जो http 500 लौटने के लिए प्रतीत होते हैं, वे इस सामान से फंसे नहीं हैं, हालांकि 404 हैं। प्रिंट स्टेटमेंट्स के साथ मैंने नीचे दिए गए कोड में छोड़ा है, मैं देख सकता हूं कि यह इनमें से किसी भी दिनचर्या को नहीं मारता है। मुझे वास्तव में क्या करना चाहिए? सब सेटोरनाडो एप्लिकेशन में आवेदन अपवादों को गहन रूप से संभालना

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

पहले, अपवाद है कि आप कोड 200 है prepare में ऊपर उठाने कर रहे हैं, इसलिए यह get_error_html समारोह में पकड़ा नहीं है।

दूसरा, get_error_html बहिष्कृत है: write_error का उपयोग करें, इसके बजाय (write_error)। प्रारंभ करने में एक हैंडलर का उपयोग initialize (initialize), लेकिन इस मामले में आप इसकी आवश्यकता नहीं है:

अंत में, आप ErrorHandler पर __init__ कॉल करने की आवश्यकता नहीं है।

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

इबुला, प्रतिक्रिया के लिए धन्यवाद। हनल्डर के लिए write_error() विधि घोषित करके मैं 500s को फंसाने में सक्षम हूं; हालांकि यह दृष्टिकोण वास्तव में साइट पर 404 के संचालन के लिए काम नहीं करता है! तो मुझे आपके द्वारा सुझाए गए एक write_error() विधि के साथ बेस क्लास के अलावा tornado.web.ErrorHandler को आवंटित एक वैश्विक त्रुटि हैंडलर क्लास को गठबंधन करना पड़ा। अब मैं वैश्विक 404 के अलावा, मेरे आवेदन अपवादों को पकड़ने में सक्षम हूं। – Karra

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!') 
  1. सेटिंग का उपयोग करने के लिए जा रहा। हम default_handler_class और default_handler_args रूप में अच्छी तरह
settings = { 
    'default_handler_class': ErrorHandler, 
    'default_handler_args': dict(status_code=404) 
} 
  1. आवेदन को परिभाषित करने की जरूरत है।
application = tornado.web.Application([ 
    (r"/", MainHandler) 
], **settings) 
परिणाम के रूप में

। 40H को छोड़कर सभी त्रुटियों को बेसहैंडलर द्वारा संभाला जाएगा। 404 - एररहैंडलर। यह है :)

-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

संबंधित मुद्दे