Custom Error Responses in Flask

Update 2012-04-08: there’s a simpler way

Python’s Flask framework leverages Werkzeug’s HTTP exceptions module to make it easy to report error states in requests. The abort method works with most common HTTP status codes, but sometimes you may want to return a code that isn’t implemented by Werkzeug.

When I’m building services on services I like to use 424 - Failed Dependency to indicate that an unhandleable error occured in a dependent service. By subclassing Werkzeug’s HTTPException I can respond with that exception even though Werkzeug itself doesn’t implement it.

In this example it doesn’t matter if Github is down or if the application is running up against the rate limit – the app will indicate that through no fault of it’s own it cannot fulfill the request.

Through a similar subclass you’ll be able to implement custom, newly proposed, and otherwise unimplemented status codes.