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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rich Churcher
  • 7,361
  • 3
  • 37
  • 60

2 Answers2

3

I think you want:

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

The path matcher also matches slashes.

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
  • This doesn't work for me. Werkzeug sees 'any' as an unexpected keyword argument. Is there an alternate syntax? – Matt Norris Mar 09 '11 at 03:25
  • Here's [the docs](http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.PathConverter). Maybe 'any' is confusing it because there's also an any matcher. You could try to see if it works. – Luke Francl Mar 09 '11 at 21:33
2

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
  • 9,338
  • 4
  • 44
  • 62
  • 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! – Rich Churcher Oct 21 '10 at 03:06