chore: log warnings for database tables api (#30410)
This commit is contained in:
parent
9a5e8a4b70
commit
2e5016713a
|
|
@ -158,7 +158,7 @@ class DatabaseTestConnectionUnexpectedError(SupersetErrorsException):
|
||||||
message = _("Unexpected error occurred, please check your logs for details")
|
message = _("Unexpected error occurred, please check your logs for details")
|
||||||
|
|
||||||
|
|
||||||
class DatabaseTablesUnexpectedError(Exception):
|
class DatabaseTablesUnexpectedError(CommandException):
|
||||||
status = 422
|
status = 422
|
||||||
message = _("Unexpected error occurred, please check your logs for details")
|
message = _("Unexpected error occurred, please check your logs for details")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ class TablesDatabaseCommand(BaseCommand):
|
||||||
except SupersetException:
|
except SupersetException:
|
||||||
raise
|
raise
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
raise DatabaseTablesUnexpectedError(ex) from ex
|
raise DatabaseTablesUnexpectedError(str(ex)) from ex
|
||||||
|
|
||||||
def validate(self) -> None:
|
def validate(self) -> None:
|
||||||
self._model = cast(Database, DatabaseDAO.find_by_id(self._db_id))
|
self._model = cast(Database, DatabaseDAO.find_by_id(self._db_id))
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ from superset.commands.database.exceptions import (
|
||||||
DatabaseDeleteFailedError,
|
DatabaseDeleteFailedError,
|
||||||
DatabaseInvalidError,
|
DatabaseInvalidError,
|
||||||
DatabaseNotFoundError,
|
DatabaseNotFoundError,
|
||||||
DatabaseTablesUnexpectedError,
|
|
||||||
DatabaseUpdateFailedError,
|
DatabaseUpdateFailedError,
|
||||||
InvalidParametersError,
|
InvalidParametersError,
|
||||||
)
|
)
|
||||||
|
|
@ -131,7 +130,7 @@ from superset.views.base_api import (
|
||||||
requires_json,
|
requires_json,
|
||||||
statsd_metrics,
|
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
|
from superset.views.filters import BaseFilterRelatedUsers, FilterRelatedOwners
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
@ -755,9 +754,9 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||||
|
|
||||||
@expose("/<int:pk>/tables/")
|
@expose("/<int:pk>/tables/")
|
||||||
@protect()
|
@protect()
|
||||||
@safe
|
|
||||||
@rison(database_tables_query_schema)
|
@rison(database_tables_query_schema)
|
||||||
@statsd_metrics
|
@statsd_metrics
|
||||||
|
@handle_api_exception
|
||||||
@event_logger.log_this_with_context(
|
@event_logger.log_this_with_context(
|
||||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".tables",
|
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".tables",
|
||||||
log_to_statsd=False,
|
log_to_statsd=False,
|
||||||
|
|
@ -810,16 +809,9 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||||
catalog_name = kwargs["rison"].get("catalog_name")
|
catalog_name = kwargs["rison"].get("catalog_name")
|
||||||
schema_name = kwargs["rison"].get("schema_name", "")
|
schema_name = kwargs["rison"].get("schema_name", "")
|
||||||
|
|
||||||
try:
|
command = TablesDatabaseCommand(pk, catalog_name, schema_name, force)
|
||||||
command = TablesDatabaseCommand(pk, catalog_name, schema_name, force)
|
payload = command.run()
|
||||||
payload = command.run()
|
return self.response(200, **payload)
|
||||||
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)
|
|
||||||
|
|
||||||
@expose("/<int:pk>/table/<path:table_name>/<schema_name>/", methods=("GET",))
|
@expose("/<int:pk>/table/<path:table_name>/<schema_name>/", methods=("GET",))
|
||||||
@protect()
|
@protect()
|
||||||
|
|
|
||||||
|
|
@ -2005,7 +2005,8 @@ class TestDatabaseApi(SupersetTestCase):
|
||||||
self.assertEqual(option["type"], "table")
|
self.assertEqual(option["type"], "table")
|
||||||
self.assertTrue(option["value"] in schemas)
|
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
|
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'})}"
|
uri = f"api/v1/database/{example_db.id}/tables/?q={prison.dumps({'schema_name': 'non_existent'})}"
|
||||||
rv = self.client.get(uri)
|
rv = self.client.get(uri)
|
||||||
self.assertEqual(rv.status_code, 404)
|
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):
|
def test_database_tables_invalid_query(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -2026,8 +2030,12 @@ class TestDatabaseApi(SupersetTestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(rv.status_code, 400)
|
self.assertEqual(rv.status_code, 400)
|
||||||
|
|
||||||
|
@patch("superset.utils.log.logger")
|
||||||
@mock.patch("superset.security.manager.SupersetSecurityManager.can_access_database")
|
@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
|
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'})}"
|
f"api/v1/database/{database.id}/tables/?q={prison.dumps({'schema_name': 'main'})}"
|
||||||
)
|
)
|
||||||
self.assertEqual(rv.status_code, 422)
|
self.assertEqual(rv.status_code, 422)
|
||||||
|
logger_mock.warning.assert_called_once_with("Test Error", exc_info=True)
|
||||||
|
|
||||||
def test_test_connection(self):
|
def test_test_connection(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue