I'm using Flask 1.0, Flask-SqlAlchemy 2 and Angular 7.
When SqlAlchemy throws an error I want to show a tailored error message in the frontend.
There is a section in the official Flask documentation about how to handle errors and also a similar question here on SO, that is related to Flask-Restless though. And yet I'm not able to connect the dots.
When SqlAlchemy throws an error, it looks something like this:
DETAIL: Key (id)=(123) is not present in table "foo".
I'm returning the error to the route:
try:
db.session.commit()
except Exception as error:
db.session.flush()
db.session.rollback()
return error
In the route I'm checking if it is an error:
if status == True:
return jsonify( { "success": True } ), 201
else:
return error_response(500, str(status))
And my error_response class looks like this:
def error_response(status_code, message=None):
payload = {"error": HTTP_STATUS_CODES.get(status_code, "Unknown error")}
if message:
payload["message"] = message
response = jsonify(payload)
response.status_code = status_code
return response
But the response json just contains a generic error message:
"message": "Http failure response for http://127.0.0.1:5000/database/add_foo: 0 Unknown Error"