Skip to content

Commit f40e283

Browse files
committed
Handle optional ipfshttpclient exception a bit more gracefully
1 parent 516f9dd commit f40e283

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ethpm/_utils/backend.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@
2828
)
2929

3030
try:
31-
from ipfshttpclient.exceptions import (
32-
ConnectionError as IpfsConnectionError,
33-
)
31+
from ipfshttpclient.exceptions import ConnectionError as IpfsConnectionError
3432
except ImportError:
3533
pass
3634

37-
logger = logging.getLogger("ethpm.utils.backend")
3835

39-
IPFS_NODE_UNAVAILABLE_MSG = "No local IPFS node available on port 5001."
36+
logger = logging.getLogger("ethpm.utils.backend")
4037

4138
ALL_URI_BACKENDS = [
4239
InfuraIPFSBackend,
@@ -47,6 +44,19 @@
4744
]
4845

4946

47+
def _handle_optional_ipfs_backend_exception(e: Exception) -> None:
48+
try:
49+
# if optional `ipfshttpclient` module is present, catch and debug if
50+
# IpfsConnectionError, else raise original exception.
51+
if isinstance(e, IpfsConnectionError):
52+
logger.debug("No local IPFS node available on port 5001.", exc_info=True)
53+
else:
54+
raise e
55+
except NameError:
56+
# if optional `ipfshttpclient` module is not present, raise original exception
57+
raise e
58+
59+
5060
@to_tuple
5161
def get_translatable_backends_for_uri(
5262
uri: URI,
@@ -56,8 +66,8 @@ def get_translatable_backends_for_uri(
5666
try:
5767
if backend().can_translate_uri(uri): # type: ignore
5868
yield backend
59-
except IpfsConnectionError:
60-
logger.debug(IPFS_NODE_UNAVAILABLE_MSG, exc_info=True)
69+
except Exception as e:
70+
_handle_optional_ipfs_backend_exception(e)
6171

6272

6373
@to_tuple
@@ -77,5 +87,5 @@ def get_resolvable_backends_for_uri(
7787
try:
7888
if backend_class().can_resolve_uri(uri): # type: ignore
7989
yield backend_class
80-
except IpfsConnectionError:
81-
logger.debug(IPFS_NODE_UNAVAILABLE_MSG, exc_info=True)
90+
except Exception as e:
91+
_handle_optional_ipfs_backend_exception(e)

0 commit comments

Comments
 (0)