@wswld

Behind the schedule since 1989

Overriding Default Werkzeug Exceptions in Flask

Update 15.06.2018: The issue described in this post is now a history, read the epic story of my struggle to get it fixed.

Let’s play a game here. What HTTP code is this exception:

{ 
  "message": "The browser (or proxy) sent a request that this server could not understand." 
}

No no, you don’t look at the code in response! That’s cheating! This is actually a default Werkzeug description for 400 code. No shit. I thought something is bad with my headers or encryption, but I would never guess simple Bad request from this message. You could use a custom exception of course, the problem is, that the very useful abort(400) object (it’s an Aborter in disguise) would stick with the default exception anyway. Let’s fix it, shall we?

There may be several possible ways of fixing that, but what I’m gonna do is just update abort.mapping. Create a separate module for your custom HTTP exceptions custom_http_exceptions.py and put a couple of overridden exceptions there (don’t forget to import abort we’ll be needing that in a moment):

from flask import abort
from werkzeug.exceptions import HTTPException


class BadRequest(HTTPException): 
    code = 400
    description = 'Bad request.'


class NotFound(HTTPException): 
    code = 404
    description = 'Resource not found.' 

These are perfectly functional, but we still need to add it to the default abort mapping:

abort.mapping.update({ 
    400: BadRequest, 
    404: NotFound 
})

Note that I import abort object from flask, not flask-restful, only the former is an Aborter object with mapping and other bells and whistles, the latter is just a function.

Now just import this module with * to your app Flask module (where you declare and run your Flask app) or someplace it would have a similar effect on runtime.

Note that you also should have the following line in your config because of this issue:

ERROR_404_HELP = False 

I’m not sure why this awkward and undocumented constant isn’t False by default. I opened an issue on GitHub, but no one seems to care.