chore: log warnings for database tables api (#30410)

This commit is contained in:
Elizabeth Thompson 2024-09-30 13:52:17 -07:00 committed by GitHub
parent 9a5e8a4b70
commit 2e5016713a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 17 deletions

View File

@ -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")

View File

@ -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))

View File

@ -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("/<int:pk>/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("/<int:pk>/table/<path:table_name>/<schema_name>/", methods=("GET",))
@protect()

View File

@ -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):
"""