Skip to content

Commit

Permalink
fix: ignore logging regular attr errs (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored May 3, 2024
1 parent 27480ce commit 4a039c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ape/utils/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ def only_raise_attribute_error(fn: Callable) -> Any:
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except Exception as e:
except AttributeError:
raise # Don't modify or log attr errors.
except Exception as err:
# Wrap the exception in AttributeError
logger.log_debug_stack_trace()
raise ApeAttributeError(f"{e}") from e
raise ApeAttributeError(f"{err}") from err

return wrapper

Expand Down
30 changes: 29 additions & 1 deletion tests/functional/utils/test_basemodel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest

from ape.exceptions import ProviderNotConnectedError
from ape.utils.basemodel import ManagerAccessMixin
from ape.logging import logger
from ape.utils.basemodel import ManagerAccessMixin, only_raise_attribute_error


class CustomClass(ManagerAccessMixin):
Expand All @@ -22,3 +23,30 @@ def test_provider_not_active(networks, accessor):
_ = accessor.provider
finally:
networks.active_provider = initial


def test_only_raise_attribute_error(mocker, ape_caplog):
spy = mocker.spy(logger, "log_debug_stack_trace")

@only_raise_attribute_error
def fn():
raise ValueError("foo bar error")

with pytest.raises(AttributeError, match="foo bar error"):
fn()

assert spy.call_count


def test_only_raise_attribute_error_when_already_raises(mocker, ape_caplog):
spy = mocker.spy(logger, "log_debug_stack_trace")

@only_raise_attribute_error
def fn():
raise AttributeError("foo bar error")

with pytest.raises(AttributeError, match="foo bar error"):
fn()

# Does not log because is already an attr err
assert not spy.call_count

0 comments on commit 4a039c2

Please sign in to comment.