The default handler when no resource matches is path_not_found responder:
But, as you can see in the _ get_responder method of the falcon API, it cannot be overridden without some monkey fixes.
As far as I can see, there are two different ways to use a custom handler:
- Subclass the API class and overwrite the _get_responder method so that it calls your custom handler
- Use the default route that matches any route if none of the applications match. You probably prefer to use sink instead of route, so you produce any HTTP method (GET, POST ...) with the same function.
I would recommend the second option, as it looks a lot neat.
Your code will look like this:
import falcon class HomeResource: def on_get(self, req, resp): resp.body = 'Hello world' def handle_404(req, resp): resp.status = falcon.HTTP_404 resp.body = 'Not found' application = falcon.API() application.add_route('/', HomeResource())
source share