-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: trap SQLAlchemy common exceptions & throw 422 error instead #19672
fix: trap SQLAlchemy common exceptions & throw 422 error instead #19672
Conversation
Codecov Report
@@ Coverage Diff @@
## master #19672 +/- ##
==========================================
+ Coverage 66.30% 66.60% +0.30%
==========================================
Files 1681 1683 +2
Lines 64408 64742 +334
Branches 6593 6593
==========================================
+ Hits 42704 43124 +420
+ Misses 20020 19934 -86
Partials 1684 1684
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
except (exc.IntegrityError, exc.DatabaseError, exc.DataError) as ex: | ||
logger.exception(ex) | ||
return json_error_response(utils.error_msg_from_exception(ex), status=422) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning 422
status code for DatabaseError
and IntegrityError
is not good. Because these errors are not related to the entity which is sent from Client side.
500
is more acceptable for these errors.
422
is good for DataError
Check here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
https://docs.sqlalchemy.org/en/14/errors.html
SUMMARY
This PR adds exception handling for the most common SQLAlchemy exceptions, returning a 422 response instead of a plain 500 error.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
not applicable
TESTING INSTRUCTIONS
This can only be verified in code, raising one of the added errors and inspecting the response, ensuring it's a 422 one.
ADDITIONAL INFORMATION