Skip to content

Commit

Permalink
Handle optional ipfshttpclient exception a bit more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Jan 13, 2023
1 parent 516f9dd commit f40e283
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions ethpm/_utils/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
)

try:
from ipfshttpclient.exceptions import (
ConnectionError as IpfsConnectionError,
)
from ipfshttpclient.exceptions import ConnectionError as IpfsConnectionError
except ImportError:
pass

logger = logging.getLogger("ethpm.utils.backend")

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

ALL_URI_BACKENDS = [
InfuraIPFSBackend,
Expand All @@ -47,6 +44,19 @@
]


def _handle_optional_ipfs_backend_exception(e: Exception) -> None:
try:
# if optional `ipfshttpclient` module is present, catch and debug if
# IpfsConnectionError, else raise original exception.
if isinstance(e, IpfsConnectionError):
logger.debug("No local IPFS node available on port 5001.", exc_info=True)
else:
raise e
except NameError:
# if optional `ipfshttpclient` module is not present, raise original exception
raise e


@to_tuple
def get_translatable_backends_for_uri(
uri: URI,
Expand All @@ -56,8 +66,8 @@ def get_translatable_backends_for_uri(
try:
if backend().can_translate_uri(uri): # type: ignore
yield backend
except IpfsConnectionError:
logger.debug(IPFS_NODE_UNAVAILABLE_MSG, exc_info=True)
except Exception as e:
_handle_optional_ipfs_backend_exception(e)


@to_tuple
Expand All @@ -77,5 +87,5 @@ def get_resolvable_backends_for_uri(
try:
if backend_class().can_resolve_uri(uri): # type: ignore
yield backend_class
except IpfsConnectionError:
logger.debug(IPFS_NODE_UNAVAILABLE_MSG, exc_info=True)
except Exception as e:
_handle_optional_ipfs_backend_exception(e)

0 comments on commit f40e283

Please sign in to comment.