开发者

Catch-all routing using Tipfy

开发者 https://www.devze.com 2023-01-21 11:07 出处:网络
Using tipfy, how does one express a catch-all route in urls.py if more 开发者_高级运维specific routes do not match?

Using tipfy, how does one express a catch-all route in urls.py if more 开发者_高级运维specific routes do not match?

Tipfy uses Werkzeug-like routing, so there's this (in urls.py):

def get_rules(app): 
rules = [ 
    Rule('/<any>', endpoint='any', handler='apps.main.handlers.MainHandler'), 
    Rule('/', endpoint='main', handler='apps.main.handlers.MainHandler'), 
] 

This will match most random entry points into the application (app.example.com/foo, app.example.com/%20 etc) but does not cover the app.example.com/foo/bar case which results in a 404.

Alternatively, is there a graceful way to handle 404 in Tipfy that I'm missing?


I think you want:

Rule('/<path:any>', endpoint='any', handler='apps.main.handlers.MainHandler')

The path matcher also matches slashes.


Maybe you could write custom middle ware:

class CustomErrorPageMiddleware(object):    
def handle_exception(self, e):           
    return Response("custom error page")

To enable it add somewhere to tipfy config:

   config['tipfy'] = {
       'middleware': [
           'apps.utils.CustomErrorPageMiddleware',
       ]
   }

It gives you quite a flexibility - you could for example send mail somewhere to inform that there was a problem. This will intercept all exceptions in your application

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号