| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
from globus_sdk._internal import guards |
| 6 |
from globus_sdk.exc import ErrorSubdocument, GlobusAPIError |
| 7 |
|
| 8 |
|
| 9 |
class TimersAPIError(GlobusAPIError): |
| 10 |
""" |
| 11 |
Error class to represent error responses from Timers. |
| 12 |
|
| 13 |
Implements a dedicated method for parsing error responses from Timers due |
| 14 |
to the differences between various error formats used. |
| 15 |
""" |
| 16 |
|
| 17 |
def _parse_undefined_error_format(self) -> bool: |
| 18 |
""" |
| 19 |
Treat any top-level "error" key as an "array of size 1". |
| 20 |
Meaning that we'll see a single subdocument for data shaped like |
| 21 |
{ |
| 22 |
"error": { |
| 23 |
"foo": "bar" |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
Error shapes also include validation errors in a 'details' array: |
| 28 |
|
| 29 |
{ |
| 30 |
"detail": [ |
| 31 |
{ |
| 32 |
"loc": ["body", "start"], |
| 33 |
"msg": "field required", |
| 34 |
"type": "value_error.missing" |
| 35 |
}, |
| 36 |
{ |
| 37 |
"loc": ["body", "callback_url"], |
| 38 |
"msg": "field required", |
| 39 |
"type": "value_error.missing" |
| 40 |
} |
| 41 |
] |
| 42 |
} |
| 43 |
""" |
| 44 |
# if there is not a top-level 'error' key and no top-level |
| 45 |
# 'detail' key, no special behavior is defined |
| 46 |
# fall-back to the base class implementation |
| 47 |
# but before that fallback, try the two relevant branches |
| 48 |
|
| 49 |
# if 'error' is present, use it to populate the errors array |
| 50 |
# extract 'code' and 'messages' from it |
| 51 |
if isinstance(self._dict_data.get("error"), dict): |
| 52 |
self.errors = [ErrorSubdocument(self._dict_data["error"])] |
| 53 |
self.code = self._extract_code_from_error_array(self.errors) |
| 54 |
self.messages = self._extract_messages_from_error_array(self.errors) |
| 55 |
return True |
| 56 |
elif guards.is_list_of(self._dict_data.get("detail"), dict): |
| 57 |
# collect the errors array from details |
| 58 |
self.errors = [ |
| 59 |
ErrorSubdocument(d, message_fields=("msg",)) |
| 60 |
for d in self._dict_data["detail"] |
| 61 |
] |
| 62 |
# extract a 'code' if there is one |
| 63 |
self.code = self._extract_code_from_error_array(self.errors) |
| 64 |
|
| 65 |
# build custom 'messages' for this case |
| 66 |
self.messages = [ |
| 67 |
f"{message}: {loc}" |
| 68 |
for (message, loc) in _parse_detail_docs(self.errors) |
| 69 |
] |
| 70 |
return True |
| 71 |
else: |
| 72 |
return super()._parse_undefined_error_format() |
| 73 |
|
| 74 |
|
| 75 |
def _parse_detail_docs( |
| 76 |
errors: list[ErrorSubdocument], |
| 77 |
) -> t.Iterator[tuple[str, str]]: |
| 78 |
for d in errors: |
| 79 |
if d.message is None: |
| 80 |
continue |
| 81 |
loc_list = d.get("loc") |
| 82 |
if not guards.is_list_of(loc_list, str): |
| 83 |
continue |
| 84 |
yield (d.message, ".".join(loc_list)) |