From daf37cbe585515afe725d266ee6ac292b2a00c44 Mon Sep 17 00:00:00 2001 From: John Bodley <4567245+john-bodley@users.noreply.github.com> Date: Thu, 13 Jun 2024 09:36:59 -0700 Subject: [PATCH] chore: Remove the need for explicit bubble up of certain exceptions (#29235) --- superset/charts/data/api.py | 11 ++++++++--- superset/commands/database/ssh_tunnel/create.py | 14 +++++++++----- superset/tasks/async_queries.py | 11 ++++++++--- superset/utils/decorators.py | 3 --- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/superset/charts/data/api.py b/superset/charts/data/api.py index 963edc2b3..ae88fdef5 100644 --- a/superset/charts/data/api.py +++ b/superset/charts/data/api.py @@ -442,10 +442,15 @@ class ChartDataRestApi(ChartRestApi): def _create_query_context_from_form( self, form_data: dict[str, Any] ) -> QueryContext: + """ + Create the query context from the form data. + + :param form_data: The chart form data + :returns: The query context + :raises ValidationError: If the request is incorrect + """ + try: return ChartDataQueryContextSchema().load(form_data) except KeyError as ex: raise ValidationError("Request is incorrect") from ex - except ValidationError: # pylint: disable=try-except-raise - # Make sure to bubble this up - raise diff --git a/superset/commands/database/ssh_tunnel/create.py b/superset/commands/database/ssh_tunnel/create.py index ea38aa52b..40083b4b6 100644 --- a/superset/commands/database/ssh_tunnel/create.py +++ b/superset/commands/database/ssh_tunnel/create.py @@ -45,15 +45,19 @@ class CreateSSHTunnelCommand(BaseCommand): self._database = database def run(self) -> Model: + """ + Create an SSH tunnel. + + :returns: The SSH tunnel model + :raises SSHTunnelCreateFailedError: If the model creation fails + :raises SSHTunnelInvalidError: If the configuration are invalid + """ + try: self.validate() - ssh_tunnel = SSHTunnelDAO.create(attributes=self._properties, commit=False) - return ssh_tunnel + return SSHTunnelDAO.create(attributes=self._properties, commit=False) except DAOCreateFailedError as ex: raise SSHTunnelCreateFailedError() from ex - except SSHTunnelInvalidError: # pylint: disable=try-except-raise - # Make sure to bubble this up - raise def validate(self) -> None: # TODO(hughhh): check to make sure the server port is not localhost diff --git a/superset/tasks/async_queries.py b/superset/tasks/async_queries.py index 5be7acc8c..2fdf09484 100644 --- a/superset/tasks/async_queries.py +++ b/superset/tasks/async_queries.py @@ -51,13 +51,18 @@ def set_form_data(form_data: dict[str, Any]) -> None: def _create_query_context_from_form(form_data: dict[str, Any]) -> QueryContext: + """ + Create the query context from the form data. + + :param form_data: The task form data + :returns: The query context + :raises ValidationError: If the request is incorrect + """ + try: return ChartDataQueryContextSchema().load(form_data) except KeyError as ex: raise ValidationError("Request is incorrect") from ex - except ValidationError: # pylint: disable=try-except-raise - # Make sure to bubble this up - raise def _load_user_from_job_metadata(job_metadata: dict[str, Any]) -> User: diff --git a/superset/utils/decorators.py b/superset/utils/decorators.py index 8e54541e9..3900bdd41 100644 --- a/superset/utils/decorators.py +++ b/superset/utils/decorators.py @@ -146,9 +146,6 @@ def stats_timing(stats_key: str, stats_logger: BaseStatsLogger) -> Iterator[floa start_ts = now_as_float() try: yield start_ts - except Exception: # pylint: disable=try-except-raise - # Make sure to bubble this up - raise finally: stats_logger.timing(stats_key, now_as_float() - start_ts)