[英]Tornado custom error - there is better way?
In Tornado there is an option to override write_error function of request handler to create your custom error page. 在Tornado中,有一个选项可以覆盖请求处理程序的write_error函数来创建自定义错误页面。
In my application there are many Handlers, and i want to create custom error page when i get code 500. I thought to implement this by create Mixin class and all my handlers will inherit this mixing. 在我的应用程序中有许多处理程序,我想在获得代码500时创建自定义错误页面。我想通过创建Mixin类来实现它,并且我的所有处理程序都将继承这种混合。
I would like to ask if there is better option to do it, maybe there is a way to configure application? 我想问一下是否有更好的选择,也许有办法配置应用程序?
My workaround looks kinda similar as you're thinking of. 我的解决方法看起来有点像你想的那样。 I have a
BaseHandler
and all my handlers inherit this class. 我有一个
BaseHandler
,我的所有处理程序都继承了这个类。
class BaseHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
"""Do your thing"""
I'm doing just like you mention. 我就像你提到的那样。 In order to do it you just have to create a class for each kind of error message and just override the write_error , like:
为了做到这一点,你只需要为每种错误消息创建一个类,然后覆盖write_error ,如:
class BaseHandler(tornado.web.RequestHandler):
def common_method(self, arg):
pass
class SpecificErrorMessageHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
if status_code == 404:
self.response(status_code,
'Resource not found. Check the URL.')
elif status_code == 405:
self.response(status_code,
'Method not allowed in this resource.')
else:
self.response(status_code,
'Internal server error on specific module.')
class ResourceHandler(BaseHandler, SpecificErrorMessageHandler):
def get(self):
pass
The final class will inherit only the specified. 最后一个类只会继承指定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.