diff --git a/CHANGES b/CHANGES index 533369d..2b1cd1f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,8 @@ main ==== +Exceptions + - `DataAPIResponseException.error_descriptors` is a property (computed from detailed_error_descriptors) + - better string representation of `DataAPIDetailedErrorDescriptor` Spawner methods for databases/admins standardized; they don't issue DevOps API calls. - removed `normalize_region_for_id` utility method, not used anymore. - `AstraDBAdmin.get_[async]_database()`: diff --git a/astrapy/exceptions/data_api_exceptions.py b/astrapy/exceptions/data_api_exceptions.py index dcc3868..bebf056 100644 --- a/astrapy/exceptions/data_api_exceptions.py +++ b/astrapy/exceptions/data_api_exceptions.py @@ -96,6 +96,11 @@ def __str__(self) -> str: return self.summary() def summary(self) -> str: + """ + Determine a string succinct description of this error descriptor. + + The precise format of this summary is determined by which fields are set. + """ non_code_part: str | None if self.title: if self.message: @@ -141,6 +146,16 @@ class DataAPIDetailedErrorDescriptor: command: dict[str, Any] | None raw_response: dict[str, Any] + def __repr__(self) -> str: + pieces = [ + f"error_descriptors={self.error_descriptors.__repr__()}" + if self.error_descriptors + else None, + "command=..." if self.command else None, + "raw_response=..." if self.raw_response else None, + ] + return f"{self.__class__.__name__}({', '.join(pc for pc in pieces if pc)})" + @dataclass class DataAPIResponseException(DataAPIException): @@ -156,9 +171,6 @@ class DataAPIResponseException(DataAPIException): Attributes: text: a text message about the exception. - error_descriptors: a list of all DataAPIErrorDescriptor objects - found across all requests involved in this exception, which are - possibly more than one. detailed_error_descriptors: a list of DataAPIDetailedErrorDescriptor objects, one for each of the requests performed during this operation. For single-request methods, such as insert_one, this list always @@ -166,21 +178,28 @@ class DataAPIResponseException(DataAPIException): """ text: str | None - error_descriptors: list[DataAPIErrorDescriptor] detailed_error_descriptors: list[DataAPIDetailedErrorDescriptor] def __init__( self, text: str | None, *, - error_descriptors: list[DataAPIErrorDescriptor], detailed_error_descriptors: list[DataAPIDetailedErrorDescriptor], ) -> None: super().__init__(text) self.text = text - self.error_descriptors = error_descriptors self.detailed_error_descriptors = detailed_error_descriptors + @property + def error_descriptors(self) -> list[DataAPIErrorDescriptor]: + """Return a flattened list of all individual DataAPIErrorDescriptor objects.""" + + return [ + error_descriptor + for d_e_d in self.detailed_error_descriptors + for error_descriptor in d_e_d.error_descriptors + ] + @classmethod def from_response( cls, @@ -219,15 +238,14 @@ def from_responses( ) detailed_error_descriptors.append(detailed_error_descriptor) - # flatten - error_descriptors = [ + flat_error_descriptors = [ error_descriptor for d_e_d in detailed_error_descriptors for error_descriptor in d_e_d.error_descriptors ] - if error_descriptors: - summaries = [e_d.summary() for e_d in error_descriptors] + if flat_error_descriptors: + summaries = [e_d.summary() for e_d in flat_error_descriptors] if len(summaries) == 1: text = summaries[0] else: @@ -241,7 +259,6 @@ def from_responses( return cls( text, - error_descriptors=error_descriptors, detailed_error_descriptors=detailed_error_descriptors, **kwargs, ) @@ -251,7 +268,6 @@ def data_api_response_exception(self) -> DataAPIResponseException: return DataAPIResponseException( text=self.text, - error_descriptors=self.error_descriptors, detailed_error_descriptors=self.detailed_error_descriptors, )