From 078b8f24b6ecaf6177c115f810e55d7fe39cba2e Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 22 Aug 2024 16:52:09 -0500 Subject: [PATCH] fix: issue missing source traceback on error when given only transaction (#2238) --- src/ape/exceptions.py | 16 ++++++++-------- src/ape/types/trace.py | 1 - tests/functional/test_exceptions.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ape/exceptions.py b/src/ape/exceptions.py index 341c2bdce0..04badd8afe 100644 --- a/src/ape/exceptions.py +++ b/src/ape/exceptions.py @@ -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() @@ -250,12 +251,16 @@ 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 @@ -263,11 +268,7 @@ 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): @@ -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 diff --git a/src/ape/types/trace.py b/src/ape/types/trace.py index d40b1afc84..f314648055 100644 --- a/src/ape/types/trace.py +++ b/src/ape/types/trace.py @@ -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: diff --git a/tests/functional/test_exceptions.py b/tests/functional/test_exceptions.py index 8bbd999672..8007b902fc 100644 --- a/tests/functional/test_exceptions.py +++ b/tests/functional/test_exceptions.py @@ -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):