Let's say you are creating a RESTful web service that typically sees JSON requests and responds with JSON back. When things go wrong, default errors that Flask/Werkzeug respond with are all HTML. Which breaks the clients who expect JSON back even in case of errors.
Here's an approach to mitigate this. All errors that Werkzeug may throw are now intercepted and converted
into JSON response. You can customize what goes into the response by tweaking the line with response = jsonify(...)
.
Also note that make_json_error
will be used when your code throws an arbitrary exception (e.g. division by zero)
not derived from HTTPException
.