| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import sys |
| 5 |
import typing as t |
| 6 |
|
| 7 |
from globus_sdk import exc |
| 8 |
|
| 9 |
from ._auth_requirements_error import GARE |
| 10 |
from ._variants import ( |
| 11 |
LegacyAuthorizationParametersError, |
| 12 |
LegacyAuthRequirementsErrorVariant, |
| 13 |
LegacyConsentRequiredAPError, |
| 14 |
LegacyConsentRequiredTransferError, |
| 15 |
LegacyDependentConsentRequiredAuthError, |
| 16 |
) |
| 17 |
|
| 18 |
if sys.version_info >= (3, 10): |
| 19 |
from typing import TypeAlias |
| 20 |
else: |
| 21 |
from typing_extensions import TypeAlias |
| 22 |
|
| 23 |
|
| 24 |
AnyErrorDocumentType: TypeAlias = ( |
| 25 |
"exc.GlobusAPIError | exc.ErrorSubdocument | dict[str, t.Any]" |
| 26 |
) |
| 27 |
|
| 28 |
log = logging.getLogger(__name__) |
| 29 |
|
| 30 |
|
| 31 |
def to_gare(error: AnyErrorDocumentType) -> GARE | None: |
| 32 |
""" |
| 33 |
Converts a GlobusAPIError, ErrorSubdocument, or dict into a |
| 34 |
GARE by attempting to match to GARE (preferred) or legacy variants. |
| 35 |
|
| 36 |
.. note:: |
| 37 |
|
| 38 |
A GlobusAPIError may contain multiple errors, and in this case only a single |
| 39 |
GARE is returned for the first error that matches a known format. |
| 40 |
|
| 41 |
|
| 42 |
If the provided error does not match a known format, None is returned. |
| 43 |
|
| 44 |
:param error: The error to convert. |
| 45 |
""" |
| 46 |
from globus_sdk.exc import ErrorSubdocument, GlobusAPIError |
| 47 |
|
| 48 |
# GlobusAPIErrors may contain more than one error, so we consider all of them |
| 49 |
# even though we only return the first. |
| 50 |
if isinstance(error, GlobusAPIError): |
| 51 |
# first, try to parse a GARE from the root document, |
| 52 |
# and if we can do so, return it |
| 53 |
authreq_error = _lenient_dict2gare(error.raw_json) |
| 54 |
if authreq_error is not None: |
| 55 |
return authreq_error |
| 56 |
|
| 57 |
# Iterate over ErrorSubdocuments |
| 58 |
for subdoc in error.errors: |
| 59 |
authreq_error = _lenient_dict2gare(subdoc.raw) |
| 60 |
if authreq_error is not None: |
| 61 |
# Return only the first auth requirements error we encounter |
| 62 |
return authreq_error |
| 63 |
|
| 64 |
# We failed to find a Globus Auth Requirements Error |
| 65 |
return None |
| 66 |
elif isinstance(error, ErrorSubdocument): |
| 67 |
return _lenient_dict2gare(error.raw) |
| 68 |
else: |
| 69 |
return _lenient_dict2gare(error) |
| 70 |
|
| 71 |
|
| 72 |
def to_gares(errors: list[AnyErrorDocumentType]) -> list[GARE]: |
| 73 |
""" |
| 74 |
Converts a list of GlobusAPIErrors, ErrorSubdocuments, or dicts into a list of |
| 75 |
GAREs by attempting to match each error to GARE (preferred) or legacy variants. |
| 76 |
|
| 77 |
.. note:: |
| 78 |
|
| 79 |
A GlobusAPIError may contain multiple errors, so the result |
| 80 |
list could be longer than the provided list. |
| 81 |
|
| 82 |
If no errors match any known formats, an empty list is returned. |
| 83 |
|
| 84 |
:param errors: The errors to convert. |
| 85 |
""" |
| 86 |
maybe_gares: list[GARE | None] = [] |
| 87 |
for error in errors: |
| 88 |
# when handling an API error, avoid `to_gare(error)` because that will |
| 89 |
# only unpack a single result |
| 90 |
if isinstance(error, exc.GlobusAPIError): |
| 91 |
# Use the ErrorSubdocuments when handling API error types |
| 92 |
maybe_gares.extend(to_gare(e) for e in error.errors) |
| 93 |
# Also use the root document, but only if there is an `"errors"` |
| 94 |
# key inside of the error document |
| 95 |
# Why? Because the *default* for `.errors` when there is no inner |
| 96 |
# `"errors"` array is an array containing the root document as a |
| 97 |
# subdocument |
| 98 |
if isinstance(error.raw_json, dict) and "errors" in error.raw_json: |
| 99 |
# use dict parsing directly so that the native descent in 'to_gare' |
| 100 |
# to subdocuments does not apply in this case |
| 101 |
maybe_gares.append(_lenient_dict2gare(error.raw_json)) |
| 102 |
else: |
| 103 |
maybe_gares.append(to_gare(error)) |
| 104 |
|
| 105 |
# Remove any errors that did not resolve to a Globus Auth Requirements Error |
| 106 |
return [error for error in maybe_gares if error is not None] |
| 107 |
|
| 108 |
|
| 109 |
def _lenient_dict2gare(error_dict: dict[str, t.Any] | None) -> GARE | None: |
| 110 |
""" |
| 111 |
Parse a GARE from a dict, accepting legacy variants. |
| 112 |
|
| 113 |
If given ``None``, returns ``None``. This allows this to accept inputs |
| 114 |
which are themselves dict|None. |
| 115 |
|
| 116 |
:param error_dict: the error input |
| 117 |
:eturns: ``None`` on a failed parse |
| 118 |
""" |
| 119 |
if error_dict is None: |
| 120 |
return None |
| 121 |
|
| 122 |
# Prefer a proper auth requirements error, if possible |
| 123 |
try: |
| 124 |
return GARE.from_dict(error_dict) |
| 125 |
except exc.ValidationError as err: |
| 126 |
log.debug(f"Failed to parse error as 'GARE' ({err})") |
| 127 |
|
| 128 |
supported_variants: list[type[LegacyAuthRequirementsErrorVariant]] = [ |
| 129 |
LegacyAuthorizationParametersError, |
| 130 |
LegacyConsentRequiredTransferError, |
| 131 |
LegacyConsentRequiredAPError, |
| 132 |
LegacyDependentConsentRequiredAuthError, |
| 133 |
] |
| 134 |
for variant in supported_variants: |
| 135 |
try: |
| 136 |
return variant.from_dict(error_dict).to_auth_requirements_error() |
| 137 |
except exc.ValidationError as err: |
| 138 |
log.debug(f"Failed to parse error as '{variant.__name__}' ({err})") |
| 139 |
|
| 140 |
return None |
| 141 |
|
| 142 |
|
| 143 |
def is_gare(error: AnyErrorDocumentType) -> bool: |
| 144 |
""" |
| 145 |
Return True if the provided error matches a known |
| 146 |
Globus Auth Requirements Error format. |
| 147 |
|
| 148 |
:param error: The error to check. |
| 149 |
""" |
| 150 |
return to_gare(error) is not None |
| 151 |
|
| 152 |
|
| 153 |
def has_gares(errors: list[AnyErrorDocumentType]) -> bool: |
| 154 |
""" |
| 155 |
Return True if any of the provided errors match a known |
| 156 |
Globus Auth Requirements Error format. |
| 157 |
|
| 158 |
:param errors: The errors to check. |
| 159 |
""" |
| 160 |
return any(is_gare(error) for error in errors) |