From c6b36b3b265bc64909b36ac6215eae8cf183ef27 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Thu, 11 Jul 2024 16:42:31 -0700 Subject: [PATCH] Make InvalidCutoutParametersError generic Add a new exception to the UWS library that represents a generic error with cutout parameter parsing and includes the parameters in the exception details. --- src/vocutouts/exceptions.py | 17 ----------------- src/vocutouts/models/cutout.py | 11 +++++------ src/vocutouts/uws/exceptions.py | 18 ++++++++++++------ 3 files changed, 17 insertions(+), 29 deletions(-) delete mode 100644 src/vocutouts/exceptions.py diff --git a/src/vocutouts/exceptions.py b/src/vocutouts/exceptions.py deleted file mode 100644 index 2fb1268..0000000 --- a/src/vocutouts/exceptions.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Exceptions for the image cutout service.""" - -from __future__ import annotations - -from .uws.exceptions import ParameterError -from .uws.models import UWSJobParameter - -__all__ = ["InvalidCutoutParameterError"] - - -class InvalidCutoutParameterError(ParameterError): - """The parameters for the cutout were invalid.""" - - def __init__(self, message: str, params: list[UWSJobParameter]) -> None: - detail = "\n".join(f"{p.parameter_id}={p.value}" for p in params) - super().__init__(message, detail) - self.params = params diff --git a/src/vocutouts/models/cutout.py b/src/vocutouts/models/cutout.py index 4d05524..9868b98 100644 --- a/src/vocutouts/models/cutout.py +++ b/src/vocutouts/models/cutout.py @@ -20,9 +20,8 @@ field_validator, ) -from ..exceptions import InvalidCutoutParameterError from ..uws.config import ParametersModel -from ..uws.exceptions import MultiValuedParameterError +from ..uws.exceptions import MultiValuedParameterError, ParameterParseError from ..uws.models import UWSJobParameter from .domain.cutout import ( WorkerCircleStencil, @@ -223,11 +222,11 @@ def from_job_parameters(cls, params: list[UWSJobParameter]) -> Self: Raises ------ - InvalidCutoutParameterError - Raised if one of the parameters could not be parsed. MultiValuedParameterError Raised if more than one dataset ID or more than one stencil is provided. + ParameterParseError + Raised if one of the parameters could not be parsed. """ ids = [] stencils = [] @@ -241,7 +240,7 @@ def from_job_parameters(cls, params: list[UWSJobParameter]) -> Self: stencils.append(stencil) except Exception as e: msg = f"Invalid cutout parameter: {type(e).__name__}: {e!s}" - raise InvalidCutoutParameterError(msg, params) from e + raise ParameterParseError(msg, params) from e # For now, only support a single ID and stencil. These have to be # checked outside of the validator because the SODA standard requires @@ -254,7 +253,7 @@ def from_job_parameters(cls, params: list[UWSJobParameter]) -> Self: try: return cls(ids=ids, stencils=stencils) except ValidationError as e: - raise InvalidCutoutParameterError(str(e), params) from e + raise ParameterParseError(str(e), params) from e def to_worker_parameters(self) -> WorkerCutout: """Convert to the domain model used by the backend worker.""" diff --git a/src/vocutouts/uws/exceptions.py b/src/vocutouts/uws/exceptions.py index 0346bfd..c3a79d3 100644 --- a/src/vocutouts/uws/exceptions.py +++ b/src/vocutouts/uws/exceptions.py @@ -1,8 +1,4 @@ -"""Exceptions for the Universal Worker Service. - -The types of exceptions here control the error handling behavior configured in -:py:mod:`vocutouts.uws.errors`. -""" +"""Exceptions for the Universal Worker Service.""" from __future__ import annotations @@ -19,7 +15,7 @@ ) from safir.slack.webhook import SlackIgnoredException -from .models import ErrorCode, ErrorType, UWSJobError +from .models import ErrorCode, ErrorType, UWSJobError, UWSJobParameter from .uwsworker import WorkerError, WorkerErrorType __all__ = [ @@ -27,6 +23,7 @@ "InvalidPhaseError", "MultiValuedParameterError", "ParameterError", + "ParameterParseError", "PermissionDeniedError", "SyncJobFailedError", "SyncJobNoResultsError", @@ -262,6 +259,15 @@ class ParameterError(UsageError): """Unsupported value passed to a parameter.""" +class ParameterParseError(ParameterError): + """UWS job parameters could not be parsed.""" + + def __init__(self, message: str, params: list[UWSJobParameter]) -> None: + detail = "\n".join(f"{p.parameter_id}={p.value}" for p in params) + super().__init__(message, detail) + self.params = params + + class UnknownJobError(DataMissingError): """The named job could not be found in the database."""