Skip to content

Commit

Permalink
fix: is_convertible error handling (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 7, 2024
1 parent 840454f commit 9ae90e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/ape/managers/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ape.api import ConverterAPI, TransactionAPI
from ape.api.address import BaseAddress
from ape.exceptions import ConversionError
from ape.logging import logger
from ape.types import AddressType
from ape.utils import cached_property, log_instead_of_fail

Expand Down Expand Up @@ -351,7 +352,17 @@ def convert(self, value: Any, type: Union[Type, Tuple, List]) -> Any:
return value

for converter in self._converters[type]:
if not converter.is_convertible(value):
try:
is_convertible = converter.is_convertible(value)
except Exception as err:
# If errors while checking if we can convert, log the error
# and assume it's not convertible.
converter_name = converter.__class__.__name__
msg = f"Issue while checking `{converter_name}.is_convertible()`: {err}"
logger.error(msg)
continue

if not is_convertible:
continue

try:
Expand Down
9 changes: 7 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ def config():


@pytest.fixture(scope="session")
def convert(chain):
return chain.conversion_manager.convert
def conversion_manager(chain):
return chain.conversion_manager


@pytest.fixture(scope="session")
def convert(conversion_manager):
return conversion_manager.convert


@pytest.fixture(scope="session")
Expand Down
29 changes: 29 additions & 0 deletions tests/functional/conversion/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any

import pytest

from ape.api import ConverterAPI
from ape.exceptions import ConversionError


def test_convert_logs_and_passes_errors_from_is_convertible(conversion_manager, ape_caplog):
"""
When checking if something is convertible, and is_convertible errors
for whatever reason, log the error and consider it "not convertible".
More than likely, it isn't by that converter and is a plugin-error.
"""
error_msg = "Unexpected error!"

class ProblematicConverter(ConverterAPI):
def is_convertible(self, value: Any) -> bool:
raise ValueError(error_msg)

def convert(self, value: Any) -> Any:
return value

conversion_manager._converters[dict] = (ProblematicConverter(),)
expected = f"Issue while checking `ProblematicConverter.is_convertible()`: {error_msg}"
with pytest.raises(ConversionError):
_ = conversion_manager.convert(123, dict)

assert expected in ape_caplog.head

0 comments on commit 9ae90e5

Please sign in to comment.