How do I return HTTP error code without default template in Tornado?

后端 未结 7 2174
情深已故
情深已故 2021-02-01 02:40

I am currently using the following to raise a HTTP bad request:

raise tornado.web.HTTPError(400)

which returns a html output:

&         


        
7条回答
  •  情深已故
    2021-02-01 03:20

    It's better to use the standard interface and define your custom message on the HTTPError.

    raise tornado.web.HTTPError(status_code=code, log_message=custom_msg)
    

    You can then parse the error in your RequestHandler and check for the message:

    class CustomHandler(tornado.web.RequestHandler):
        def write_error(self, status_code, **kwargs):
            err_cls, err, traceback = kwargs['exc_info']
            if err.log_message and err.log_message.startswith(custom_msg):
                self.write("

    Here be dragons

    ")

提交回复
热议问题