Skip to content
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

Merged

Conversation

diegomedina248
Copy link
Contributor

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

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@codecov
Copy link

codecov bot commented Apr 12, 2022

Codecov Report

Merging #19672 (bdc2268) into master (d693f4e) will increase coverage by 0.30%.
The diff coverage is 76.92%.

@@            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              
Flag Coverage Δ
hive 52.88% <76.92%> (?)
mysql 82.01% <76.92%> (+0.11%) ⬆️
postgres 82.05% <76.92%> (+0.11%) ⬆️
presto 52.72% <76.92%> (?)
python 82.49% <76.92%> (+0.45%) ⬆️
sqlite 81.83% <76.92%> (+0.12%) ⬆️
unit 47.87% <76.92%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/views/base.py 73.77% <25.00%> (-0.74%) ⬇️
superset/views/base_api.py 98.38% <100.00%> (+0.06%) ⬆️
superset/sql_lab.py 78.90% <0.00%> (-2.74%) ⬇️
superset/security/api.py 96.47% <0.00%> (-0.03%) ⬇️
superset/importexport/api.py 100.00% <0.00%> (ø)
superset/databases/filters.py 100.00% <0.00%> (ø)
superset/utils/pandas_postprocessing/compare.py 90.32% <0.00%> (ø)
superset/utils/pandas_postprocessing/flatten.py 100.00% <0.00%> (ø)
superset/embedded_dashboard/commands/exceptions.py 100.00% <0.00%> (ø)
superset/embedded/api.py 93.33% <0.00%> (ø)
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d693f4e...bdc2268. Read the comment docs.

Comment on lines +234 to +236
except (exc.IntegrityError, exc.DatabaseError, exc.DataError) as ex:
logger.exception(ex)
return json_error_response(utils.error_msg_from_exception(ex), status=422)
Copy link
Contributor

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

@diegomedina248 diegomedina248 marked this pull request as draft April 12, 2022 21:01
@diegomedina248 diegomedina248 marked this pull request as ready for review April 15, 2022 18:19
@rusackas rusackas merged commit 4513cc4 into apache:master Apr 20, 2022
hughhhh pushed a commit to hve-labs/superset that referenced this pull request May 11, 2022
philipher29 pushed a commit to ValtechMobility/superset that referenced this pull request Jun 9, 2022
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 2.0.0 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels Preset-Patch size/S 🚢 2.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants