views:

85

answers:

2

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?

+3  A: 

I think you want:

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

The path matcher also matches slashes.

Luke Francl
Nice one, thank you very much.
EloquentGeek
+1  A: 

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

Michal Sznajder
This is a nice idea actually. I'm only accepting the other answer because it more specifically addresses the question of matching the URL, but your point is well-taken. Thanks!
EloquentGeek