title: Custom abort mapping/exceptions in Flask
body: <p>The default message for Flask <code>400</code> exception (<code>abort()</code>) is:</p>
  
  <pre><code>{
    "message": "The browser (or proxy) sent a request that this server could not understand."
  }
  </code></pre>
  
  <p>For <code>404</code>:</p>
  
  <pre><code>{
    "message": "The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again. You have requested this URI [/obj/] but did you mean /obj/ or /obj/&lt;int:id&gt;/ or /obj/&lt;int:id&gt;/kill/ ?"
  }
  </code></pre>
  
  <p>I have trouble comprehending these messages when I'm getting them as replies in my API (especially the first one, I thought there's something wrong with encryption or headers) and I thing it's kinda tiresome to try to override text manually for every <code>abort()</code> exception. So I change the mapping:</p>
  
  <pre><code>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.'
  
  
  abort.mapping.update({
      400: BadRequest,
      404: NotFound
  })
  </code></pre>
  
  <p>For the case of <code>400</code> it works beautifully. But when it comes to <code>404</code> it is still the same message. I tested it in the same place in my code - it works for <code>abort(400)</code>, <code>abort(403)</code> and some of the others, but it gets mysteriously overridden by default message on <code>abort(404)</code>. Debugging didn't help much. What may be the culprit here?</p>
  
  <p><strong>Update</strong>. Yes, I'm using <code>abort</code> imported from <code>flask</code> not <code>flask_restful</code> as the latter doesn't have the mapping and it's a function not an <code>Aborter</code> object. Besides, it does work for most exceptions, so it's probably not the real issue here.</p>
  
  <p><strong>Update 2</strong>. The <code>abort.mapping</code> seems to be perfectly fine on execution. The exceptions in question are overridden, including <code>404</code>.
  <a href="https://i.stack.imgur.com/K7Vr0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K7Vr0.png" alt="Proofpic"></a></p>
  
  <p><strike><strong>Update 3</strong>: I've put together a little sandbox, that I use for debugging.</strike> (removed the repo since the mystery is long solved).</p>