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: issue missing source traceback on error when given only transaction #2238

Merged
merged 1 commit into from
Aug 22, 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
16 changes: 8 additions & 8 deletions src/ape/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def __init__(
# Finalizes expected revert message.
super().__init__(ex_message)

self._attempted_source_traceback = False
if set_ape_traceback:
self.with_ape_traceback()

Expand Down Expand Up @@ -250,24 +251,24 @@ def trace(self, value):
def source_traceback(self) -> Optional["SourceTraceback"]:
tb = self._source_traceback
result: Optional["SourceTraceback"]
if callable(tb):
if not self._attempted_source_traceback and tb is None and self.txn is not None:
result = _get_ape_traceback_from_tx(self.txn)
# Prevent re-trying.
self._attempted_source_traceback = True
elif callable(tb):
result = tb()
self._source_traceback = result
else:
result = tb

self._source_traceback = result
return result

@source_traceback.setter
def source_traceback(self, value):
self._source_traceback = value

def _get_ape_traceback(self) -> Optional[TracebackType]:
source_tb = self.source_traceback
if not source_tb and self.txn:
source_tb = _get_ape_traceback_from_tx(self.txn)

if src_tb := source_tb:
if src_tb := self.source_traceback:
# Create a custom Pythonic traceback using lines from the sources
# found from analyzing the trace of the transaction.
if py_tb := _get_custom_python_traceback(self, src_tb, project=self._project):
Expand Down Expand Up @@ -335,7 +336,6 @@ def dev_message(self) -> Optional[str]:
Raises:
``ValueError``: When unable to get dev message.
"""

return self.source_traceback.revert_type if self.source_traceback else None

@classmethod
Expand Down
1 change: 0 additions & 1 deletion src/ape/types/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ def revert_type(self) -> Optional[str]:
The revert type, such as a builtin-error code or a user dev-message,
if there is one.
"""

return self.statements[-1].type if self.statements[-1].type != "source" else None

def append(self, __object) -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ def assert_ape_traceback(err_arg):

assert_ape_traceback(err3)

def test_source_traceback_from_txn(self, owner):
"""
Was not given a source-traceback but showing we can deduce one from
the given transaction.
"""
tx = owner.transfer(owner, 0)
err = TransactionError(txn=tx)
_ = err.source_traceback
assert err._attempted_source_traceback


class TestNetworkNotFoundError:
def test_close_match(self):
Expand Down
Loading