V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
fanzeyi
V2EX  ›  Tornado

你们是如何自定义HTTP Error页面的?

  •  
  •   fanzeyi · 2012-01-11 00:46:55 +08:00 · 7270 次点击
    这是一个创建于 4460 天前的主题,其中的信息可能已经有所发展或是发生改变。
    找到两种方法:

    1. 重写 tornado.web.ErrorHandler ... 测试了下这种只能 catch 到普通的 404 错误... 更多的比如 raise tornado.web.HTTPError(404) 这样的就处理不了

    2. 就是在人工 raise 错误前... 生成一个错误页面.. 然后用 nginx 之类的转向到这个页面.. 但是一旦同时出现错误... 会导致各种混乱..

    我觉得这两种方法都不是很完美... 有更好的方法么?

    (正在扒拉Tornado源码的某人..
    7 条回复    1970-01-01 08:00:00 +08:00
    fanzeyi
        1
    fanzeyi  
    OP
       2012-01-11 00:50:50 +08:00
    简单看了下Tornado的代码.. 似乎可以通过重写 tornado.web.HTTPError 来实现?
    Livid
        2
    Livid  
    MOD
       2012-01-11 00:51:57 +08:00
    override write_error
    fanzeyi
        3
    fanzeyi  
    OP
       2012-01-11 00:57:50 +08:00
    @Livid got it.. thanks..

    其实不用重写 write_error ... 直接在 BaseHandler 中提供一个 get_error_html 函数就可以处理了

    get_error_html(self, status_code, **kwargs)

    thanks
    tioover
        4
    tioover  
       2012-05-05 22:18:19 +08:00
    补充一条,
    如果tornado 没有找到匹配的url最后会出现很丑的默认错误页面
    所以

    http://gist.github.com/2602795
    Janpo
        5
    Janpo  
       2012-05-10 10:53:19 +08:00
    In Tornado 2.0 and earlier, custom error pages were implemented by overriding RequestHandler.get_error_html, which returned the error page as a string instead of calling the normal output methods (and had slightly different semantics for exceptions). This method is still supported, but it is deprecated and applications are encouraged to switch to RequestHandler.write_error
    CoX
        6
    CoX  
       2012-05-10 11:24:40 +08:00
    习惯是set_status(404),然后render的,是不是不正规呢?
    fsw90628
        7
    fsw90628  
       2012-05-10 15:16:42 +08:00
    @livid 这个 http://v2ex.appspot.com/ 有解决办法不?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3166 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 12:26 · PVG 20:26 · LAX 05:26 · JFK 08:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.