Skip to content

Commit

Permalink
chore: log multiple errors (#14064)
Browse files Browse the repository at this point in the history
* log all errors from db create

* return unique set of errors

* sort set for exceptions list

* run black

(cherry picked from commit c143b37)
  • Loading branch information
eschutho authored and villebro committed Apr 3, 2022
1 parent e97b123 commit 253f80a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions superset/commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def add(self, exception: ValidationError) -> None:
def add_list(self, exceptions: List[ValidationError]) -> None:
self._invalid_exceptions.extend(exceptions)

def get_list_classnames(self) -> List[str]:
return list(sorted({ex.__class__.__name__ for ex in self._invalid_exceptions}))

def normalized_messages(self) -> Dict[Any, Any]:
errors: Dict[Any, Any] = {}
for exception in self._invalid_exceptions:
Expand Down
5 changes: 4 additions & 1 deletion superset/databases/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def validate(self) -> None:
exception = DatabaseInvalidError()
exception.add_list(exceptions)
event_logger.log_with_context(
action=f"db_connection_failed.{exception.__class__.__name__}"
action="db_connection_failed.{}.{}".format(
exception.__class__.__name__,
".".join(exception.get_list_classnames()),
)
)
raise exception
39 changes: 39 additions & 0 deletions tests/integration_tests/databases/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from superset.commands.exceptions import CommandInvalidError
from superset.commands.importers.exceptions import IncorrectVersionError
from superset.connectors.sqla.models import SqlaTable
from superset.databases.commands.create import CreateDatabaseCommand
from superset.databases.commands.exceptions import (
DatabaseInvalidError,
DatabaseNotFoundError,
DatabaseSecurityUnsafeError,
DatabaseTestConnectionDriverError,
Expand Down Expand Up @@ -64,6 +66,43 @@
)


class TestCreateDatabaseCommand(SupersetTestCase):
@mock.patch(
"superset.databases.commands.test_connection.event_logger.log_with_context"
)
def test_create_duplicate_error(self, mock_logger):
example_db = get_example_database()
command = CreateDatabaseCommand(
security_manager.find_user("admin"),
{"database_name": example_db.database_name},
)
with pytest.raises(DatabaseInvalidError) as excinfo:
command.run()
assert str(excinfo.value) == ("Database parameters are invalid.")
# logger should list classnames of all errors
mock_logger.assert_called_with(
action="db_connection_failed."
"DatabaseInvalidError."
"DatabaseExistsValidationError."
"DatabaseRequiredFieldValidationError"
)

@mock.patch(
"superset.databases.commands.test_connection.event_logger.log_with_context"
)
def test_multiple_error_logging(self, mock_logger):
command = CreateDatabaseCommand(security_manager.find_user("admin"), {})
with pytest.raises(DatabaseInvalidError) as excinfo:
command.run()
assert str(excinfo.value) == ("Database parameters are invalid.")
# logger should list a unique set of errors with no duplicates
mock_logger.assert_called_with(
action="db_connection_failed."
"DatabaseInvalidError."
"DatabaseRequiredFieldValidationError"
)


class TestExportDatabasesCommand(SupersetTestCase):
@skip("Flaky")
@patch("superset.security.manager.g")
Expand Down

0 comments on commit 253f80a

Please sign in to comment.