Search code examples
pythongoogle-app-enginepython-2.7webapp2

Show 404 when specific route matches


I have the following routes setup, matching a rest api on /rest.

routes = [
    PathPrefixRoute('/rest', [
        Route('/customers', handler='app.handlers.CustomerHandler:list',
                            methods=['GET']),
        # some other /rest routers
        Route('/<:.*>', ?) # this route should execute abort(404)
    ]),
    Route('/<page:.*>', handler='app.handlers.PageHandler', methods=['GET'])
]

If somebody accesses a /rest/does_not_exists url I want to show 404.

Is there a way to invoke abort(404) from a route or should I create a handler for that?


Solution

  • Since you have a catch all route just like the page and if this is in the same context of last question you can do

    if not os.path.exists(os.path.join(template_path, page)): 
        self.abort(404)
    

    But yeah you need to create handler to call self.abort on specific routes although un-caught routes does exactly this by default.