I'd like to be able to display custom error messages using abort() in Flask. I have run into an unusual situation that I am having trouble with. My code is below.
from flask import Flask, abort, jsonify, make_response, request
app = Flask(__name__)
@app.errorhandler(400)
def custom400(error):
return make_response(jsonify({'message': error.description}), 400)
@app.route('/test', methods=['POST'])
def test():
abort(400, 'custom error message')
if __name__ == '__main__':
app.run()
The code above works but behaves differently if I attempt to access any form data because I lose access to the custom error message.
def test():
a = request.form['some_value']
abort(400, 'custom error message')
How can I avoid this situation? Is this a Flask bug?
Note: The code above was taken from how to get access to error message from abort command when using custom error handler