I am using exception handling classes e.g., werkzeug.exceptions.BadRequest
to raise exceptions when they occur. I want to return a JSON to the user when an exception is encountered. For example,
{
'http_response': 400,
'message':'error message'
}
By default, the user gets an HTML error message like:
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>error message</p>
Should I create a custom subclass of BadRequest and JSONify the response there? I am reluctant to do it this way because I would have to create subclasses for all the types of exceptions there are. Is there any existing library or a better way to do this?