From b1bbc23528ea57d6bd49cadf5d7be0b856de7306 Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 29 Aug 2024 09:25:53 -0500 Subject: [PATCH] fix: Change None check to Falsey check for ContractLogicError message to be the TB revert type (#2253) --- src/ape/exceptions.py | 4 +--- tests/functional/test_exceptions.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ape/exceptions.py b/src/ape/exceptions.py index 04badd8afe..c26ee6eca5 100644 --- a/src/ape/exceptions.py +++ b/src/ape/exceptions.py @@ -305,7 +305,6 @@ def __init__( ): self.txn = txn self.contract_address = contract_address - super().__init__( base_err=base_err, contract_address=contract_address, @@ -316,8 +315,7 @@ def __init__( trace=trace, txn=txn, ) - - if revert_message is None and source_traceback is not None and (dev := self.dev_message): + if not revert_message and source_traceback is not None and (dev := self.dev_message): try: # Attempt to use dev message as main exception message. self.message = dev diff --git a/tests/functional/test_exceptions.py b/tests/functional/test_exceptions.py index 8007b902fc..55f3ca9d36 100644 --- a/tests/functional/test_exceptions.py +++ b/tests/functional/test_exceptions.py @@ -1,5 +1,6 @@ import re from pathlib import Path +from typing import Optional import pytest @@ -11,6 +12,7 @@ TransactionError, handle_ape_exception, ) +from ape.types import SourceTraceback from ape_ethereum.transactions import DynamicFeeTransaction, Receipt @@ -207,3 +209,18 @@ def test_handle_ape_exception_hides_home_dir(mocker): # We expect the same only the home dir has been hidden. expected = "\n" + tb_str.replace(str(Path.home()), "$HOME") assert actual == expected + + +class TestContractLogicError: + @pytest.mark.parametrize("revert_message", (None, "")) + def test_message_uses_revert_type_when_no_revert_message(self, mocker, revert_message): + class TB(SourceTraceback): + @property + def revert_type(self) -> Optional[str]: + return "CUSTOM_ERROR" + + tb = TB([{"statements": [], "closure": {"name": "fn"}, "depth": 0}]) # type: ignore + error = ContractLogicError(revert_message=revert_message, source_traceback=tb) + actual = error.message + expected = "CUSTOM_ERROR" + assert actual == expected