Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore debug-logging regular AttributeErrors #2054

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading