Skip to content

Commit

Permalink
DataAPIErrorDescriptor, improvements (#341)
Browse files Browse the repository at this point in the history
* DataAPIResponseException.error_descriptors made into a property

* formatting
  • Loading branch information
hemidactylus authored Feb 12, 2025
1 parent 81329ab commit cb49d8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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()`:
Expand Down
40 changes: 28 additions & 12 deletions astrapy/exceptions/data_api_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -156,31 +171,35 @@ 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
has a single element.
"""

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,
Expand Down Expand Up @@ -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:
Expand All @@ -241,7 +259,6 @@ def from_responses(

return cls(
text,
error_descriptors=error_descriptors,
detailed_error_descriptors=detailed_error_descriptors,
**kwargs,
)
Expand All @@ -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,
)

Expand Down

0 comments on commit cb49d8d

Please sign in to comment.