diff --git a/docs/static/resources/openapi.json b/docs/static/resources/openapi.json index 8a31b8c39978..fd99b1b23353 100644 --- a/docs/static/resources/openapi.json +++ b/docs/static/resources/openapi.json @@ -24344,6 +24344,9 @@ "404": { "$ref": "#/components/responses/404" }, + "422": { + "$ref": "#/components/responses/422" + }, "500": { "$ref": "#/components/responses/500" } diff --git a/superset/databases/api.py b/superset/databases/api.py index 79ff53a7174f..b691946ff20b 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -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,7 +1230,27 @@ 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: + # 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 queries) raises here - that's the target + # 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. + 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)) return self.response(200, **payload) diff --git a/tests/integration_tests/databases/api_tests.py b/tests/integration_tests/databases/api_tests.py index ecdfdd876f5d..82e03a151288 100644 --- a/tests/integration_tests/databases/api_tests.py +++ b/tests/integration_tests/databases/api_tests.py @@ -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 + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_get_table_extra_metadata_deprecated(self): """