Repository navigation
fix(api): return 422 instead of 500 when table_metadata reflection fails #46380
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
Changes from 3 commits
ecd9aa0
3751d08
66ed22a
bdea933
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1197,6 +1197,8 @@ def table_metadata(self, pk: int) -> FlaskResponse: | |||||||||||||||||||||||||||
| $ref: '#/components/responses/401' | ||||||||||||||||||||||||||||
| 404: | ||||||||||||||||||||||||||||
| $ref: '#/components/responses/404' | ||||||||||||||||||||||||||||
| 422: | ||||||||||||||||||||||||||||
| $ref: '#/components/responses/422' | ||||||||||||||||||||||||||||
| 500: | ||||||||||||||||||||||||||||
| $ref: '#/components/responses/500' | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
|
|
@@ -1228,8 +1230,30 @@ def table_metadata(self, pk: int) -> FlaskResponse: | |||||||||||||||||||||||||||
| # Partition info is engine-agnostic at this layer: the generic dispatch | ||||||||||||||||||||||||||||
| # passes it to the engine spec, which decides whether to use it. Non-ODPS | ||||||||||||||||||||||||||||
| # specs ignore the parameter. | ||||||||||||||||||||||||||||
| payload = database.db_engine_spec.get_table_metadata(database, table, partition) | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| payload = database.db_engine_spec.get_table_metadata( | ||||||||||||||||||||||||||||
| database, table, partition | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| except SQLAlchemyError as ex: | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: This also catches Assessment: 🟠 Prompt for AI Agent 🤖This is a comment left during a code review.
**Path:** superset/databases/api.py
**Line:** 1236:1236
**Comment:**
*Api Mismatch: This also catches `OperationalError` during a database outage, returning 422 instead of the 500 used for connection failures and making transient failures look permanent.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed: a refused connection raises
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Outages now return 422 instead of 500 A refused or dropped connection now returns 422 with a WARNING, so 5xx alerting no longer sees outages on this endpoint.
Suggested change
|
||||||||||||||||||||||||||||
| # Reflection (get_pk_constraint/get_foreign_keys/get_indexes/ | ||||||||||||||||||||||||||||
| # get_table_comment) issues catalog queries tailored to the | ||||||||||||||||||||||||||||
| # declared dialect. A customer database that only partially | ||||||||||||||||||||||||||||
| # implements that dialect (e.g. a postgres-wire-protocol engine | ||||||||||||||||||||||||||||
| # missing pg_catalog.pg_index columns SQLAlchemy's postgres | ||||||||||||||||||||||||||||
| # dialect now queries) raises here - that's the target | ||||||||||||||||||||||||||||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||
| # database's limitation, not a Superset bug: answer with a | ||||||||||||||||||||||||||||
| # typed 422 and a warning instead of an unhandled 500 + ERROR, | ||||||||||||||||||||||||||||
| # matching the sibling table_metadata_deprecated endpoint above. | ||||||||||||||||||||||||||||
| self.incr_stats("error", self.table_metadata.__name__) | ||||||||||||||||||||||||||||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||
| "table_metadata: reflection failed for database=%s table=%s", | ||||||||||||||||||||||||||||
| pk, | ||||||||||||||||||||||||||||
| table_name, | ||||||||||||||||||||||||||||
| exc_info=True, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| return self.response_422(error_msg_from_exception(ex)) | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Optional: guest error sanitization is skipped on this path On master the 500 handler gives guests the generic "An error occurred while fetching the data.". |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self.incr_stats("success", self.table_metadata.__name__) | ||||||||||||||||||||||||||||
| return self.response(200, **payload) | ||||||||||||||||||||||||||||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @expose("/<int:pk>/table_metadata/extra/", methods=["GET"]) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2069,6 +2069,27 @@ def test_get_table_metadata_no_db_permission(self): | |||||||
| rv = self.client.get(uri) | ||||||||
| assert rv.status_code == 404 | ||||||||
|
|
||||||||
| def test_get_table_metadata_reflection_error(self): | ||||||||
| """ | ||||||||
| Database API: Test get table metadata returns 422, not 500, when the | ||||||||
| target database's reflection (pk/fk/index/comment introspection) raises | ||||||||
| a SQLAlchemyError, e.g. a postgres-wire-protocol engine missing catalog | ||||||||
| columns SQLAlchemy's postgres dialect queries. | ||||||||
| """ | ||||||||
| example_db = get_example_database() | ||||||||
| self.login(ADMIN_USERNAME) | ||||||||
| uri = ( | ||||||||
| f"api/v1/database/{example_db.id}/table_metadata/" | ||||||||
| "?name=birth_names&schema=null" | ||||||||
| ) | ||||||||
| with mock.patch.object( | ||||||||
| example_db.db_engine_spec, | ||||||||
| "get_table_metadata", | ||||||||
| side_effect=SQLAlchemyError("Binder Error: Referenced table not found"), | ||||||||
| ): | ||||||||
| rv = self.client.get(uri) | ||||||||
| assert rv.status_code == 422 | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Optional: also assert the response body, so the test pins the message as well as the status.
Suggested change
|
||||||||
|
|
||||||||
| @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") | ||||||||
| def test_get_table_extra_metadata_deprecated(self): | ||||||||
| """ | ||||||||
|
|
||||||||
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.
🟢 Optional: a missing table could return 404
NoSuchTableErroris also aSQLAlchemyError, so a missing table returns 422 here. Mapping it to the existingTableNotFoundExceptiongives the same 404 "No such table" as the access-denied branch above, so the response doesn't reveal whether the table exists.