| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
import requests |
| 6 |
|
| 7 |
from .base import GlobusError |
| 8 |
|
| 9 |
# Wrappers around requests exceptions, so the SDK is somewhat independent from details |
| 10 |
# about requests |
| 11 |
|
| 12 |
|
| 13 |
class NetworkError(GlobusError): |
| 14 |
""" |
| 15 |
Error communicating with the REST API server. |
| 16 |
|
| 17 |
Holds onto original exception data, but also takes a message |
| 18 |
to explain potentially confusing or inconsistent exceptions passed to us |
| 19 |
""" |
| 20 |
|
| 21 |
def __init__(self, msg: str, exc: Exception, *args: t.Any, **kwargs: t.Any) -> None: |
| 22 |
super().__init__(msg) |
| 23 |
self.underlying_exception = exc |
| 24 |
|
| 25 |
|
| 26 |
class GlobusTimeoutError(NetworkError): |
| 27 |
"""The REST request timed out.""" |
| 28 |
|
| 29 |
|
| 30 |
class GlobusConnectionTimeoutError(GlobusTimeoutError): |
| 31 |
"""The request timed out during connection establishment. |
| 32 |
These errors are safe to retry.""" |
| 33 |
|
| 34 |
|
| 35 |
class GlobusConnectionError(NetworkError): |
| 36 |
"""A connection error occurred while making a REST request.""" |
| 37 |
|
| 38 |
|
| 39 |
def convert_request_exception(exc: requests.RequestException) -> GlobusError: |
| 40 |
""" |
| 41 |
Converts incoming requests.Exception to a Globus NetworkError |
| 42 |
|
| 43 |
:param exc: The exception to "convert" by wrapping it |
| 44 |
""" |
| 45 |
|
| 46 |
if isinstance(exc, requests.ConnectTimeout): |
| 47 |
return GlobusConnectionTimeoutError("ConnectTimeoutError on request", exc) |
| 48 |
if isinstance(exc, requests.Timeout): |
| 49 |
return GlobusTimeoutError("TimeoutError on request", exc) |
| 50 |
elif isinstance(exc, requests.ConnectionError): |
| 51 |
return GlobusConnectionError("ConnectionError on request", exc) |
| 52 |
else: |
| 53 |
return NetworkError("NetworkError on request", exc) |