Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: log multiple errors #14064

Merged
merged 4 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we sort the list of class names, so that the exceptions with the same classes get grouped together?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good.. is it more or less helpful to see both errors if they are identical? How about filtering out duplicates?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think filtering duplicates make sense.

Suggested change
".".join(exception.get_list_classnames()),
".".join(sorted(exception.get_list_classnames())),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we filter out duplicates, do we still need to sort?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion, I added the sort to the get_list_classnames method above.

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