Skip to content

Commit ae7f5fe

Browse files
committed
Add specific ValidationError for each module that inherits from eth_utils.exceptions.ValidationError
1 parent 3e77455 commit ae7f5fe

33 files changed

+123
-102
lines changed

docs/contracts.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ Each Contract Factory exposes the following methods.
215215
216216
.. py:classmethod:: Contract.constructor(*args, **kwargs).build_transaction(transaction=None)
217217
:noindex:
218-
218+
219219
Construct the contract deploy transaction bytecode data.
220220

221221
If the contract takes constructor parameters they should be provided as
@@ -435,7 +435,7 @@ and the arguments are ambiguous.
435435
}
436436
"""
437437
# fast forward all the steps of compiling and deploying the contract.
438-
>>> ambiguous_contract.functions.identity(1, True) # raises ValidationError
438+
>>> ambiguous_contract.functions.identity(1, True) # raises Web3ValidationError
439439
440440
>>> identity_func = ambiguous_contract.get_function_by_signature('identity(uint256,bool)')
441441
>>> identity_func(1, True)
@@ -654,7 +654,7 @@ Taking the following contract code as an example:
654654
>>> array_contract.functions.setBytes2Value([b'a']).transact()
655655
Traceback (most recent call last):
656656
...
657-
ValidationError:
657+
Web3ValidationError:
658658
Could not identify the intended function with name `setBytes2Value`
659659

660660

ens/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from eth_utils.exceptions import (
2+
ValidationError,
3+
)
14
import idna
25

36

@@ -110,3 +113,9 @@ class UnderfundedBid(ENSException):
110113
"""
111114

112115
pass
116+
117+
118+
class ENSValidationError(ENSException, ValidationError):
119+
"""
120+
Raised if there is a validation error
121+
"""

ens/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
HexStr,
2525
)
2626
from eth_utils import (
27-
ValidationError,
2827
is_same_address,
2928
remove_0x_prefix,
3029
to_bytes,
@@ -48,6 +47,7 @@
4847
)
4948
from ens.exceptions import (
5049
InvalidName,
50+
ENSValidationError,
5151
)
5252

5353
default = object()
@@ -142,7 +142,9 @@ def ens_encode_name(name: str) -> bytes:
142142
# raises if len(label) > 255:
143143
for index, label in enumerate(labels):
144144
if len(label) > 255:
145-
raise ValidationError(f"Label at position {index} too long after encoding.")
145+
raise ENSValidationError(
146+
f"Label at position {index} too long after encoding."
147+
)
146148

147149
# concat label size in bytes to each label:
148150
dns_prepped_labels = [to_bytes(len(label)) + label for label in labels_as_bytes]

ethpm/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from eth_utils.exceptions import (
2+
ValidationError,
3+
)
4+
5+
16
class EthPMException(Exception):
27
"""
38
Base class for all Py-EthPM errors.
@@ -15,7 +20,7 @@ class InsufficientAssetsError(EthPMException):
1520
pass
1621

1722

18-
class EthPMValidationError(EthPMException):
23+
class EthPMValidationError(EthPMException, ValidationError):
1924
"""
2025
Raised when something does not pass a validation check.
2126
"""

tests/core/contracts/test_contract_build_transaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
)
66

77
from web3.exceptions import (
8-
ValidationError,
8+
Web3ValidationError,
99
)
1010

1111

@@ -28,7 +28,7 @@ def test_build_transaction_not_paying_to_nonpayable_function(
2828
def test_build_transaction_paying_to_nonpayable_function(
2929
w3, payable_tester_contract, build_transaction
3030
):
31-
with pytest.raises(ValidationError):
31+
with pytest.raises(Web3ValidationError):
3232
build_transaction(
3333
contract=payable_tester_contract,
3434
contract_function="doNoValueCall",
@@ -277,7 +277,7 @@ async def test_async_build_transaction_not_paying_to_nonpayable_function(
277277
async def test_async_build_transaction_paying_to_nonpayable_function(
278278
async_w3, async_payable_tester_contract, async_build_transaction
279279
):
280-
with pytest.raises(ValidationError):
280+
with pytest.raises(Web3ValidationError):
281281
await async_build_transaction(
282282
contract=async_payable_tester_contract,
283283
contract_function="doNoValueCall",

tests/core/contracts/test_contract_call_interface.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
NameNotFound,
3333
NoABIFound,
3434
NoABIFunctionsFound,
35-
ValidationError,
35+
Web3ValidationError,
3636
)
3737

3838
MULTIPLE_FUNCTIONS = json.loads(
@@ -317,7 +317,7 @@ def test_set_strict_byte_array(strict_arrays_contract, call, transact, args, exp
317317
def test_set_strict_byte_array_with_invalid_args(
318318
strict_arrays_contract, transact, args
319319
):
320-
with pytest.raises(ValidationError):
320+
with pytest.raises(Web3ValidationError):
321321
transact(
322322
contract=strict_arrays_contract,
323323
contract_function="setByteValue",
@@ -644,13 +644,13 @@ def test_no_functions_match_identifier(arrays_contract):
644644

645645
def test_function_1_match_identifier_wrong_number_of_args(arrays_contract):
646646
regex = message_regex + diagnosis_arg_regex
647-
with pytest.raises(ValidationError, match=regex):
647+
with pytest.raises(Web3ValidationError, match=regex):
648648
arrays_contract.functions.setBytes32Value().call()
649649

650650

651651
def test_function_1_match_identifier_wrong_args_encoding(arrays_contract):
652652
regex = message_regex + diagnosis_encoding_regex
653-
with pytest.raises(ValidationError, match=regex):
653+
with pytest.raises(Web3ValidationError, match=regex):
654654
arrays_contract.functions.setBytes32Value("dog").call()
655655

656656

@@ -665,7 +665,7 @@ def test_function_1_match_identifier_wrong_args_encoding(arrays_contract):
665665
def test_function_multiple_error_diagnoses(w3, arg1, arg2, diagnosis):
666666
Contract = w3.eth.contract(abi=MULTIPLE_FUNCTIONS)
667667
regex = message_regex + diagnosis
668-
with pytest.raises(ValidationError, match=regex):
668+
with pytest.raises(Web3ValidationError, match=regex):
669669
if arg2:
670670
Contract.functions.a(arg1, arg2).call()
671671
else:
@@ -683,7 +683,7 @@ def test_function_wrong_args_for_tuple_collapses_args_in_message(
683683
address,
684684
tuple_contract,
685685
):
686-
with pytest.raises(ValidationError) as e:
686+
with pytest.raises(Web3ValidationError) as e:
687687
tuple_contract.functions.method(
688688
(1, [2, 3], [(4, [True, [False]], [address])])
689689
).call()
@@ -711,7 +711,7 @@ def test_function_wrong_args_for_tuple_collapses_args_in_message(
711711
def test_function_wrong_args_for_tuple_collapses_kwargs_in_message(
712712
address, tuple_contract
713713
):
714-
with pytest.raises(ValidationError) as e:
714+
with pytest.raises(Web3ValidationError) as e:
715715
tuple_contract.functions.method(
716716
a=(1, [2, 3], [(4, [True, [False]], [address])]) # noqa: E501
717717
).call()
@@ -747,7 +747,7 @@ def test_call_not_sending_ether_to_nonpayable_function(payable_tester_contract,
747747

748748

749749
def test_call_sending_ether_to_nonpayable_function(payable_tester_contract, call):
750-
with pytest.raises(ValidationError):
750+
with pytest.raises(Web3ValidationError):
751751
call(
752752
contract=payable_tester_contract,
753753
contract_function="doNoValueCall",
@@ -817,7 +817,7 @@ def test_invalid_fixed_value_reflections(
817817
fixed_reflection_contract, function, value, error
818818
):
819819
contract_func = fixed_reflection_contract.functions[function]
820-
with pytest.raises(ValidationError, match=error):
820+
with pytest.raises(Web3ValidationError, match=error):
821821
contract_func(value).call({"gas": 420000})
822822

823823

@@ -1154,7 +1154,7 @@ async def test_async_set_strict_byte_array(
11541154
async def test_async_set_strict_byte_array_with_invalid_args(
11551155
async_strict_arrays_contract, async_transact, args
11561156
):
1157-
with pytest.raises(ValidationError):
1157+
with pytest.raises(Web3ValidationError):
11581158
await async_transact(
11591159
contract=async_strict_arrays_contract,
11601160
contract_function="setByteValue",
@@ -1514,7 +1514,7 @@ async def test_async_function_1_match_identifier_wrong_number_of_args(
15141514
async_arrays_contract,
15151515
):
15161516
regex = message_regex + diagnosis_arg_regex
1517-
with pytest.raises(ValidationError, match=regex):
1517+
with pytest.raises(Web3ValidationError, match=regex):
15181518
await async_arrays_contract.functions.setBytes32Value().call()
15191519

15201520

@@ -1523,7 +1523,7 @@ async def test_async_function_1_match_identifier_wrong_args_encoding(
15231523
async_arrays_contract,
15241524
):
15251525
regex = message_regex + diagnosis_encoding_regex
1526-
with pytest.raises(ValidationError, match=regex):
1526+
with pytest.raises(Web3ValidationError, match=regex):
15271527
await async_arrays_contract.functions.setBytes32Value("dog").call()
15281528

15291529

@@ -1539,7 +1539,7 @@ async def test_async_function_1_match_identifier_wrong_args_encoding(
15391539
async def test_async_function_multiple_diagnoses(async_w3, arg1, arg2, diagnosis):
15401540
Contract = async_w3.eth.contract(abi=MULTIPLE_FUNCTIONS)
15411541
regex = message_regex + diagnosis
1542-
with pytest.raises(ValidationError, match=regex):
1542+
with pytest.raises(Web3ValidationError, match=regex):
15431543
if arg2:
15441544
await Contract.functions.a(arg1, arg2).call()
15451545
else:
@@ -1574,7 +1574,7 @@ async def test_async_call_not_sending_ether_to_nonpayable_function(
15741574
async def test_async_call_sending_ether_to_nonpayable_function(
15751575
async_payable_tester_contract, async_call
15761576
):
1577-
with pytest.raises(ValidationError):
1577+
with pytest.raises(Web3ValidationError):
15781578
await async_call(
15791579
contract=async_payable_tester_contract,
15801580
contract_function="doNoValueCall",
@@ -1648,7 +1648,7 @@ async def test_async_invalid_fixed_value_reflections(
16481648
async_fixed_reflection_contract, function, value, error
16491649
):
16501650
contract_func = async_fixed_reflection_contract.functions[function]
1651-
with pytest.raises(ValidationError, match=error):
1651+
with pytest.raises(Web3ValidationError, match=error):
16521652
await contract_func(value).call({"gas": 420000})
16531653

16541654

tests/core/contracts/test_contract_estimate_gas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from web3.exceptions import (
4-
ValidationError,
4+
Web3ValidationError,
55
)
66

77

@@ -63,7 +63,7 @@ def test_estimate_gas_not_sending_ether_to_nonpayable_function(
6363
def test_estimate_gas_sending_ether_to_nonpayable_function(
6464
w3, payable_tester_contract, estimate_gas
6565
):
66-
with pytest.raises(ValidationError):
66+
with pytest.raises(Web3ValidationError):
6767
estimate_gas(
6868
contract=payable_tester_contract,
6969
contract_function="doNoValueCall",
@@ -169,7 +169,7 @@ async def test_async_estimate_gas_not_sending_ether_to_nonpayable_function(
169169
async def test_async_estimate_gas_sending_ether_to_nonpayable_function(
170170
async_w3, async_payable_tester_contract, async_estimate_gas
171171
):
172-
with pytest.raises(ValidationError):
172+
with pytest.raises(Web3ValidationError):
173173
await async_estimate_gas(
174174
contract=async_payable_tester_contract,
175175
contract_function="doNoValueCall",

tests/core/contracts/test_contract_method_abi_encoding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
constants,
66
)
77
from web3.exceptions import (
8-
ValidationError,
8+
Web3ValidationError,
99
)
1010

1111
ABI_A = json.loads(
@@ -112,7 +112,7 @@ def test_contract_abi_encoding_kwargs(w3):
112112
)
113113
def test_contract_abi_encoding_strict_with_error(w3_strict_abi, arguments):
114114
contract = w3_strict_abi.eth.contract(abi=ABI_C)
115-
with pytest.raises(ValidationError):
115+
with pytest.raises(Web3ValidationError):
116116
contract.encodeABI("a", arguments, data=None)
117117

118118

tests/core/contracts/test_contract_method_to_argument_matching.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ReceiveFn,
1010
)
1111
from web3.exceptions import (
12-
ValidationError,
12+
Web3ValidationError,
1313
)
1414

1515
SINGLE_FN_NO_ARGS = json.loads(
@@ -137,7 +137,7 @@ def test_finds_receive_function(w3):
137137
def test_error_when_no_function_name_match(w3):
138138
Contract = w3.eth.contract(abi=SINGLE_FN_NO_ARGS)
139139

140-
with pytest.raises(ValidationError):
140+
with pytest.raises(Web3ValidationError):
141141
Contract._find_matching_fn_abi("no_function_name", [1234])
142142

143143

@@ -176,15 +176,15 @@ def test_finds_function_with_matching_args_deprecation_warning(w3):
176176
def test_error_when_duplicate_match(w3):
177177
Contract = w3.eth.contract(abi=MULTIPLE_FUNCTIONS)
178178

179-
with pytest.raises(ValidationError):
179+
with pytest.raises(Web3ValidationError):
180180
Contract._find_matching_fn_abi("a", [100])
181181

182182

183183
@pytest.mark.parametrize("arguments", (["0xf00b47"], [b""], [""], ["00" * 16]))
184184
def test_strict_errors_if_type_is_wrong(w3_strict_abi, arguments):
185185
Contract = w3_strict_abi.eth.contract(abi=MULTIPLE_FUNCTIONS)
186186

187-
with pytest.raises(ValidationError):
187+
with pytest.raises(Web3ValidationError):
188188
Contract._find_matching_fn_abi("a", arguments)
189189

190190

tests/core/contracts/test_contract_transact_interface.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
empty,
99
)
1010
from web3.exceptions import (
11-
ValidationError,
11+
Web3ValidationError,
1212
)
1313

1414

@@ -51,7 +51,7 @@ def test_transact_sending_ether_to_nonpayable_function(
5151
)
5252

5353
assert initial_value is False
54-
with pytest.raises(ValidationError):
54+
with pytest.raises(Web3ValidationError):
5555
txn_hash = transact(
5656
contract=payable_tester_contract,
5757
contract_function="doNoValueCall",
@@ -314,7 +314,7 @@ async def test_async_transact_sending_ether_to_nonpayable_function(
314314
)
315315

316316
assert initial_value is False
317-
with pytest.raises(ValidationError):
317+
with pytest.raises(Web3ValidationError):
318318
txn_hash = await async_transact(
319319
contract=async_payable_tester_contract,
320320
contract_function="doNoValueCall",

0 commit comments

Comments
 (0)