Skip to content

Commit cb49d8d

Browse files
authored
DataAPIErrorDescriptor, improvements (#341)
* DataAPIResponseException.error_descriptors made into a property * formatting
1 parent 81329ab commit cb49d8d

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
main
22
====
3+
Exceptions
4+
- `DataAPIResponseException.error_descriptors` is a property (computed from detailed_error_descriptors)
5+
- better string representation of `DataAPIDetailedErrorDescriptor`
36
Spawner methods for databases/admins standardized; they don't issue DevOps API calls.
47
- removed `normalize_region_for_id` utility method, not used anymore.
58
- `AstraDBAdmin.get_[async]_database()`:

astrapy/exceptions/data_api_exceptions.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ def __str__(self) -> str:
9696
return self.summary()
9797

9898
def summary(self) -> str:
99+
"""
100+
Determine a string succinct description of this error descriptor.
101+
102+
The precise format of this summary is determined by which fields are set.
103+
"""
99104
non_code_part: str | None
100105
if self.title:
101106
if self.message:
@@ -141,6 +146,16 @@ class DataAPIDetailedErrorDescriptor:
141146
command: dict[str, Any] | None
142147
raw_response: dict[str, Any]
143148

149+
def __repr__(self) -> str:
150+
pieces = [
151+
f"error_descriptors={self.error_descriptors.__repr__()}"
152+
if self.error_descriptors
153+
else None,
154+
"command=..." if self.command else None,
155+
"raw_response=..." if self.raw_response else None,
156+
]
157+
return f"{self.__class__.__name__}({', '.join(pc for pc in pieces if pc)})"
158+
144159

145160
@dataclass
146161
class DataAPIResponseException(DataAPIException):
@@ -156,31 +171,35 @@ class DataAPIResponseException(DataAPIException):
156171
157172
Attributes:
158173
text: a text message about the exception.
159-
error_descriptors: a list of all DataAPIErrorDescriptor objects
160-
found across all requests involved in this exception, which are
161-
possibly more than one.
162174
detailed_error_descriptors: a list of DataAPIDetailedErrorDescriptor
163175
objects, one for each of the requests performed during this operation.
164176
For single-request methods, such as insert_one, this list always
165177
has a single element.
166178
"""
167179

168180
text: str | None
169-
error_descriptors: list[DataAPIErrorDescriptor]
170181
detailed_error_descriptors: list[DataAPIDetailedErrorDescriptor]
171182

172183
def __init__(
173184
self,
174185
text: str | None,
175186
*,
176-
error_descriptors: list[DataAPIErrorDescriptor],
177187
detailed_error_descriptors: list[DataAPIDetailedErrorDescriptor],
178188
) -> None:
179189
super().__init__(text)
180190
self.text = text
181-
self.error_descriptors = error_descriptors
182191
self.detailed_error_descriptors = detailed_error_descriptors
183192

193+
@property
194+
def error_descriptors(self) -> list[DataAPIErrorDescriptor]:
195+
"""Return a flattened list of all individual DataAPIErrorDescriptor objects."""
196+
197+
return [
198+
error_descriptor
199+
for d_e_d in self.detailed_error_descriptors
200+
for error_descriptor in d_e_d.error_descriptors
201+
]
202+
184203
@classmethod
185204
def from_response(
186205
cls,
@@ -219,15 +238,14 @@ def from_responses(
219238
)
220239
detailed_error_descriptors.append(detailed_error_descriptor)
221240

222-
# flatten
223-
error_descriptors = [
241+
flat_error_descriptors = [
224242
error_descriptor
225243
for d_e_d in detailed_error_descriptors
226244
for error_descriptor in d_e_d.error_descriptors
227245
]
228246

229-
if error_descriptors:
230-
summaries = [e_d.summary() for e_d in error_descriptors]
247+
if flat_error_descriptors:
248+
summaries = [e_d.summary() for e_d in flat_error_descriptors]
231249
if len(summaries) == 1:
232250
text = summaries[0]
233251
else:
@@ -241,7 +259,6 @@ def from_responses(
241259

242260
return cls(
243261
text,
244-
error_descriptors=error_descriptors,
245262
detailed_error_descriptors=detailed_error_descriptors,
246263
**kwargs,
247264
)
@@ -251,7 +268,6 @@ def data_api_response_exception(self) -> DataAPIResponseException:
251268

252269
return DataAPIResponseException(
253270
text=self.text,
254-
error_descriptors=self.error_descriptors,
255271
detailed_error_descriptors=self.detailed_error_descriptors,
256272
)
257273

0 commit comments

Comments
 (0)