I'm currently writing a REST API for an app I'm working on. The app is written in python using flask. I have the following:
try:
_profile = profile(
name=request.json['name'],
password=profile.get_salted_password('blablabla'),
email=request.json['email'],
created_by=1,
last_updated_by=1
)
except AssertionError:
abort(400)
session = DatabaseEngine.getSession()
session.add(_profile)
try:
session.commit()
except IntegrityError:
abort(400)
The error handler looks like this:
@app.errorhandler(400)
def not_found(error):
return make_response(standard_response(None, 400, 'Bad request'), 400)
I'm using the error 400 to denote both a problem with a sqlalchemy model validator and a unique constraint when writing to the database and in both cases the following error is sent to the client:
{
"data": null,
"error": {
"msg": "Bad request",
"no": 400
},
"success": false
}
Is there a way to still use abort(400) but also set the error somehow so that the error handler can take care of adding additional information for the error object in the result?
I would like it to be more in line with:
{
"data": null,
"error": {
"msg": "(IntegrityError) duplicate key value violates unique constraint profile_email_key",
"no": 400
},
"success": false
}