Tornado custom error handler for static file

How can I show the 404 error page for static files?

in my current app handler I added the last template as follows

[
    (r'/(favicon.ico)', tornado.web.StaticFileHandler, {"path": mypath}),
    (r'/foo',FooHandler),
    (r'/bar',BarHandler),
    (r'/(.*)',ErrorHandler),
]

      

and this is my error handler

class ErrorHandler(BaseHandler):

    def get(self,d):
        self.status_code = 404
        self.show404(d)

      

If I visit http://localhost/abc

I get my 404 page

but if i try to get http://localhost/static/abc.js

i get ugly error like below

line 2286, in validate_absolute_path
raise HTTPError(404)
HTTPError: HTTP 404: Not Found 

      

Is there a way to make this work? How can I show my custom error page for static files

+3


source to share


1 answer


It's a little tricky; you need to subclass StaticFileHandler and override its write_error method and then set that class with the static_handler_class app setting.



+1


source







All Articles