Skip to content

Commit 2a20a19

Browse files
committed
Use ENS exception wrappers for Python exceptions
1 parent bddb557 commit 2a20a19

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

ens/async_ens.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
from ens.exceptions import (
4848
AddressMismatch,
49+
ENSValueError,
4950
ResolverNotFound,
5051
UnauthorizedError,
5152
UnownedName,
@@ -64,9 +65,6 @@
6465
normalize_name,
6566
raw_name_to_hash,
6667
)
67-
from web3.exceptions import (
68-
Web3ValueError,
69-
)
7068

7169
if TYPE_CHECKING:
7270
from web3.contract.async_contract import ( # noqa: F401
@@ -207,7 +205,7 @@ async def setup_address(
207205
elif is_binary_address(address):
208206
address = to_checksum_address(cast(str, address))
209207
elif not is_checksum_address(address):
210-
raise Web3ValueError("You must supply the address in checksum format")
208+
raise ENSValueError("You must supply the address in checksum format")
211209
if await self.address(name) == address:
212210
return None
213211
if address is None:
@@ -285,7 +283,7 @@ async def setup_name(
285283
if is_binary_address(address):
286284
address = to_checksum_address(address)
287285
if not is_checksum_address(address):
288-
raise Web3ValueError("You must supply the address in checksum format")
286+
raise ENSValueError("You must supply the address in checksum format")
289287
await self._assert_control(address, name)
290288
if not resolved:
291289
await self.setup_address(name, address, transact=transact)

ens/ens.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
HexBytes,
3131
)
3232

33-
from web3.exceptions import (
34-
Web3ValueError,
35-
)
36-
3733
from . import (
3834
abis,
3935
)
@@ -50,6 +46,7 @@
5046
)
5147
from .exceptions import (
5248
AddressMismatch,
49+
ENSValueError,
5350
ResolverNotFound,
5451
UnauthorizedError,
5552
UnownedName,
@@ -212,7 +209,7 @@ def setup_address(
212209
elif is_binary_address(address):
213210
address = to_checksum_address(cast(str, address))
214211
elif not is_checksum_address(address):
215-
raise Web3ValueError("You must supply the address in checksum format")
212+
raise ENSValueError("You must supply the address in checksum format")
216213
if self.address(name) == address:
217214
return None
218215
if address is None:
@@ -288,7 +285,7 @@ def setup_name(
288285
if is_binary_address(address):
289286
address = to_checksum_address(address)
290287
if not is_checksum_address(address):
291-
raise Web3ValueError("You must supply the address in checksum format")
288+
raise ENSValueError("You must supply the address in checksum format")
292289
self._assert_control(address, name)
293290
if not resolved:
294291
self.setup_address(name, address, transact=transact)

ens/exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ class ENSException(Exception):
1212
pass
1313

1414

15+
class ENSValueError(ENSException, ValueError):
16+
"""
17+
An ENS exception wrapper for `ValueError`, for better control over
18+
exception handling.
19+
"""
20+
21+
pass
22+
23+
24+
class ENSTypeError(ENSException, TypeError):
25+
"""
26+
An ENS exception wrapper for `TypeError`, for better control over
27+
exception handling.
28+
"""
29+
30+
pass
31+
32+
1533
class AddressMismatch(ENSException):
1634
"""
1735
In order to set up reverse resolution correctly, the ENS name should first

ens/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
HexBytes,
3636
)
3737

38-
from web3.exceptions import (
39-
Web3TypeError,
40-
Web3ValueError,
38+
from ens.exceptions import (
39+
ENSTypeError,
40+
ENSValueError,
4141
)
4242

4343
from .constants import (
@@ -204,7 +204,7 @@ def sha3_text(val: Union[str, bytes]) -> HexBytes:
204204
def label_to_hash(label: str) -> HexBytes:
205205
label = normalize_name(label)
206206
if "." in label:
207-
raise Web3ValueError(f"Cannot generate hash for label {label!r} with a '.'")
207+
raise ENSValueError(f"Cannot generate hash for label {label!r} with a '.'")
208208
return Web3().keccak(text=label)
209209

210210

@@ -269,7 +269,7 @@ def assert_signer_in_modifier_kwargs(modifier_kwargs: Any) -> ChecksumAddress:
269269

270270
_modifier_type, modifier_dict = dict(modifier_kwargs).popitem()
271271
if "from" not in modifier_dict:
272-
raise Web3TypeError(ERR_MSG)
272+
raise ENSTypeError(ERR_MSG)
273273

274274
return modifier_dict["from"]
275275

0 commit comments

Comments
 (0)