diff --git a/superset/commands/database/exceptions.py b/superset/commands/database/exceptions.py index 216252c70..5285deb0f 100644 --- a/superset/commands/database/exceptions.py +++ b/superset/commands/database/exceptions.py @@ -158,7 +158,7 @@ class DatabaseTestConnectionUnexpectedError(SupersetErrorsException): message = _("Unexpected error occurred, please check your logs for details") -class DatabaseTablesUnexpectedError(Exception): +class DatabaseTablesUnexpectedError(CommandException): status = 422 message = _("Unexpected error occurred, please check your logs for details") diff --git a/superset/commands/database/tables.py b/superset/commands/database/tables.py index 80f174889..b28d4f065 100644 --- a/superset/commands/database/tables.py +++ b/superset/commands/database/tables.py @@ -130,7 +130,7 @@ class TablesDatabaseCommand(BaseCommand): except SupersetException: raise except Exception as ex: - raise DatabaseTablesUnexpectedError(ex) from ex + raise DatabaseTablesUnexpectedError(str(ex)) from ex def validate(self) -> None: self._model = cast(Database, DatabaseDAO.find_by_id(self._db_id)) diff --git a/superset/databases/api.py b/superset/databases/api.py index b58e46bf3..88188bed5 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -41,7 +41,6 @@ from superset.commands.database.exceptions import ( DatabaseDeleteFailedError, DatabaseInvalidError, DatabaseNotFoundError, - DatabaseTablesUnexpectedError, DatabaseUpdateFailedError, InvalidParametersError, ) @@ -131,7 +130,7 @@ from superset.views.base_api import ( requires_json, statsd_metrics, ) -from superset.views.error_handling import json_error_response +from superset.views.error_handling import handle_api_exception, json_error_response from superset.views.filters import BaseFilterRelatedUsers, FilterRelatedOwners logger = logging.getLogger(__name__) @@ -755,9 +754,9 @@ class DatabaseRestApi(BaseSupersetModelRestApi): @expose("//tables/") @protect() - @safe @rison(database_tables_query_schema) @statsd_metrics + @handle_api_exception @event_logger.log_this_with_context( action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".tables", log_to_statsd=False, @@ -810,16 +809,9 @@ class DatabaseRestApi(BaseSupersetModelRestApi): catalog_name = kwargs["rison"].get("catalog_name") schema_name = kwargs["rison"].get("schema_name", "") - try: - command = TablesDatabaseCommand(pk, catalog_name, schema_name, force) - payload = command.run() - return self.response(200, **payload) - except DatabaseNotFoundError: - return self.response_404() - except SupersetException as ex: - return self.response(ex.status, message=ex.message) - except DatabaseTablesUnexpectedError as ex: - return self.response_422(ex.message) + command = TablesDatabaseCommand(pk, catalog_name, schema_name, force) + payload = command.run() + return self.response(200, **payload) @expose("//table///", methods=("GET",)) @protect() diff --git a/tests/integration_tests/databases/api_tests.py b/tests/integration_tests/databases/api_tests.py index bd99733d9..c5cd20b5d 100644 --- a/tests/integration_tests/databases/api_tests.py +++ b/tests/integration_tests/databases/api_tests.py @@ -2005,7 +2005,8 @@ class TestDatabaseApi(SupersetTestCase): self.assertEqual(option["type"], "table") self.assertTrue(option["value"] in schemas) - def test_database_tables_not_found(self): + @patch("superset.utils.log.logger") + def test_database_tables_not_found(self, logger_mock): """ Database API: Test database tables not found """ @@ -2014,6 +2015,9 @@ class TestDatabaseApi(SupersetTestCase): uri = f"api/v1/database/{example_db.id}/tables/?q={prison.dumps({'schema_name': 'non_existent'})}" rv = self.client.get(uri) self.assertEqual(rv.status_code, 404) + logger_mock.warning.assert_called_once_with( + "Database not found.", exc_info=True + ) def test_database_tables_invalid_query(self): """ @@ -2026,8 +2030,12 @@ class TestDatabaseApi(SupersetTestCase): ) self.assertEqual(rv.status_code, 400) + @patch("superset.utils.log.logger") @mock.patch("superset.security.manager.SupersetSecurityManager.can_access_database") - def test_database_tables_unexpected_error(self, mock_can_access_database): + @mock.patch("superset.models.core.Database.get_all_table_names_in_schema") + def test_database_tables_unexpected_error( + self, mock_get_all_table_names_in_schema, mock_can_access_database, logger_mock + ): """ Database API: Test database tables with unexpected error """ @@ -2039,6 +2047,7 @@ class TestDatabaseApi(SupersetTestCase): f"api/v1/database/{database.id}/tables/?q={prison.dumps({'schema_name': 'main'})}" ) self.assertEqual(rv.status_code, 422) + logger_mock.warning.assert_called_once_with("Test Error", exc_info=True) def test_test_connection(self): """