Skip to content

Commit

Permalink
fix: issue missing source traceback on error when given only transact…
Browse files Browse the repository at this point in the history
…ion (ApeWorX#2238)
  • Loading branch information
antazoey authored Aug 22, 2024
1 parent 786d4dc commit 078b8f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
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

0 comments on commit 078b8f2

Please sign in to comment.