Skip to content

Commit

Permalink
chore: Refactor command exceptions (#24117)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley authored May 19, 2023
1 parent 0496779 commit 8b4222f
Show file tree
Hide file tree
Showing 22 changed files with 44 additions and 76 deletions.
4 changes: 1 addition & 3 deletions superset/annotation_layers/annotations/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,4 @@ def validate(self) -> None:
exceptions.append(AnnotationDatesValidationError())

if exceptions:
exception = AnnotationInvalidError()
exception.add_list(exceptions)
raise exception
raise AnnotationInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/annotation_layers/annotations/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,4 @@ def validate(self) -> None:
exceptions.append(AnnotationDatesValidationError())

if exceptions:
exception = AnnotationInvalidError()
exception.add_list(exceptions)
raise exception
raise AnnotationInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/annotation_layers/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ def validate(self) -> None:
exceptions.append(AnnotationLayerNameUniquenessValidationError())

if exceptions:
exception = AnnotationLayerInvalidError()
exception.add_list(exceptions)
raise exception
raise AnnotationLayerInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/annotation_layers/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,4 @@ def validate(self) -> None:
exceptions.append(AnnotationLayerNameUniquenessValidationError())

if exceptions:
exception = AnnotationLayerInvalidError()
exception.add_list(exceptions)
raise exception
raise AnnotationLayerInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/charts/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,4 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = ChartInvalidError()
exception.add_list(exceptions)
raise exception
raise ChartInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/charts/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,4 @@ def validate(self) -> None:
self._properties["dashboards"] = dashboards

if exceptions:
exception = ChartInvalidError()
exception.add_list(exceptions)
raise exception
raise ChartInvalidError(exceptions=exceptions)
20 changes: 12 additions & 8 deletions superset/commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,26 @@ class CommandInvalidError(CommandException):

status = 422

def __init__(self, message: str = "") -> None:
self._invalid_exceptions: List[ValidationError] = []
def __init__(
self,
message: str = "",
exceptions: Optional[List[ValidationError]] = None,
) -> None:
self._exceptions = exceptions or []
super().__init__(message)

def add(self, exception: ValidationError) -> None:
self._invalid_exceptions.append(exception)
def append(self, exception: ValidationError) -> None:
self._exceptions.append(exception)

def add_list(self, exceptions: List[ValidationError]) -> None:
self._invalid_exceptions.extend(exceptions)
def extend(self, exceptions: List[ValidationError]) -> None:
self._exceptions.extend(exceptions)

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

def normalized_messages(self) -> Dict[Any, Any]:
errors: Dict[Any, Any] = {}
for exception in self._invalid_exceptions:
for exception in self._exceptions:
errors.update(exception.normalized_messages())
return errors

Expand Down
7 changes: 4 additions & 3 deletions superset/commands/importers/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ def validate(self) -> None:
self._prevent_overwrite_existing_model(exceptions)

if exceptions:
exception = CommandInvalidError(f"Error importing {self.model_name}")
exception.add_list(exceptions)
raise exception
raise CommandInvalidError(
f"Error importing {self.model_name}",
exceptions,
)

def _prevent_overwrite_existing_model( # pylint: disable=invalid-name
self, exceptions: List[ValidationError]
Expand Down
7 changes: 4 additions & 3 deletions superset/commands/importers/v1/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def validate(self) -> None:
)

if exceptions:
exception = CommandInvalidError("Error importing assets")
exception.add_list(exceptions)
raise exception
raise CommandInvalidError(
"Error importing assets",
exceptions,
)
8 changes: 2 additions & 6 deletions superset/dashboards/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = DashboardInvalidError()
exception.add_list(exceptions)
raise exception
raise DashboardInvalidError(exceptions=exceptions)

try:
roles = populate_roles(role_ids)
self._properties["roles"] = roles
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = DashboardInvalidError()
exception.add_list(exceptions)
raise exception
raise DashboardInvalidError(exceptions=exceptions)
8 changes: 2 additions & 6 deletions superset/dashboards/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = DashboardInvalidError()
exception.add_list(exceptions)
raise exception
raise DashboardInvalidError(exceptions=exceptions)

# Validate/Populate role
if roles_ids is None:
Expand All @@ -105,6 +103,4 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = DashboardInvalidError()
exception.add_list(exceptions)
raise exception
raise DashboardInvalidError(exceptions=exceptions)
2 changes: 1 addition & 1 deletion superset/databases/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def validate(self) -> None:
exceptions.append(DatabaseExistsValidationError())
if exceptions:
exception = DatabaseInvalidError()
exception.add_list(exceptions)
exception.extend(exceptions)
event_logger.log_with_context(
action="db_connection_failed.{}.{}".format(
exception.__class__.__name__,
Expand Down
4 changes: 1 addition & 3 deletions superset/databases/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,4 @@ def validate(self) -> None:
):
exceptions.append(DatabaseExistsValidationError())
if exceptions:
exception = DatabaseInvalidError()
exception.add_list(exceptions)
raise exception
raise DatabaseInvalidError(exceptions=exceptions)
2 changes: 1 addition & 1 deletion superset/databases/ssh_tunnel/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def validate(self) -> None:
exceptions.append(SSHTunnelRequiredFieldValidationError("private_key"))
if exceptions:
exception = SSHTunnelInvalidError()
exception.add_list(exceptions)
exception.extend(exceptions)
event_logger.log_with_context(
action="ssh_tunnel_creation_failed.{}.{}".format(
exception.__class__.__name__,
Expand Down
6 changes: 3 additions & 3 deletions superset/databases/ssh_tunnel/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def validate(self) -> None:
"private_key_password"
)
if private_key_password and private_key is None:
exception = SSHTunnelInvalidError()
exception.add(SSHTunnelRequiredFieldValidationError("private_key"))
raise exception
raise SSHTunnelInvalidError(
exceptions=[SSHTunnelRequiredFieldValidationError("private_key")]
)
4 changes: 1 addition & 3 deletions superset/datasets/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,4 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = DatasetInvalidError()
exception.add_list(exceptions)
raise exception
raise DatasetInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/datasets/commands/duplicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,4 @@ def validate(self) -> None:
exceptions.append(ex)

if exceptions:
exception = DatasetInvalidError()
exception.add_list(exceptions)
raise exception
raise DatasetInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/datasets/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ def validate(self) -> None:
self._validate_metrics(metrics, exceptions)

if exceptions:
exception = DatasetInvalidError()
exception.add_list(exceptions)
raise exception
raise DatasetInvalidError(exceptions=exceptions)

def _validate_columns(
self, columns: List[Dict[str, Any]], exceptions: List[ValidationError]
Expand Down
4 changes: 1 addition & 3 deletions superset/reports/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = ReportScheduleInvalidError()
exception.add_list(exceptions)
raise exception
raise ReportScheduleInvalidError(exceptions=exceptions)

def _validate_report_extra(self, exceptions: List[ValidationError]) -> None:
extra: Optional[ReportScheduleExtra] = self._properties.get("extra")
Expand Down
4 changes: 1 addition & 3 deletions superset/reports/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,4 @@ def validate(self) -> None:
except ValidationError as ex:
exceptions.append(ex)
if exceptions:
exception = ReportScheduleInvalidError()
exception.add_list(exceptions)
raise exception
raise ReportScheduleInvalidError(exceptions=exceptions)
4 changes: 1 addition & 3 deletions superset/tags/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ def validate(self) -> None:
TagCreateFailedError(f"invalid object type {self._object_type}")
)
if exceptions:
exception = TagInvalidError()
exception.add_list(exceptions)
raise exception
raise TagInvalidError(exceptions=exceptions)
8 changes: 2 additions & 6 deletions superset/tags/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def validate(self) -> None:
)
)
if exceptions:
exception = TagInvalidError()
exception.add_list(exceptions)
raise exception
raise TagInvalidError(exceptions=exceptions)


class DeleteTagsCommand(DeleteMixin, BaseCommand):
Expand All @@ -110,6 +108,4 @@ def validate(self) -> None:
if not TagDAO.find_by_name(tag):
exceptions.append(TagNotFoundError(tag))
if exceptions:
exception = TagInvalidError()
exception.add_list(exceptions)
raise exception
raise TagInvalidError(exceptions=exceptions)

0 comments on commit 8b4222f

Please sign in to comment.