Skip to content

Commit 80b7701

Browse files
jpickclowes
authored andcommitted
Add base Web3Exception, ENSError, and PyEthPMError
Exception mixin inherited by all exceptions of each module. This allows: try: some_call() except Web3Exception as e: # deal with web3 exception except: # deal with other exceptions
1 parent d786101 commit 80b7701

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

ens/exceptions.py

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

33

4-
class AddressMismatch(ValueError):
4+
class ENSError(Exception):
5+
"""
6+
Base class for all ENS Errors
7+
"""
8+
9+
pass
10+
11+
12+
class AddressMismatch(ENSError):
513
"""
614
In order to set up reverse resolution correctly, the ENS name should first
715
point to the address. This exception is raised if the name does
@@ -11,7 +19,7 @@ class AddressMismatch(ValueError):
1119
pass
1220

1321

14-
class InvalidName(idna.IDNAError):
22+
class InvalidName(idna.IDNAError, ENSError):
1523
"""
1624
This exception is raised if the provided name does not meet
1725
the syntax standards specified in `EIP 137 name syntax
@@ -23,7 +31,7 @@ class InvalidName(idna.IDNAError):
2331
pass
2432

2533

26-
class UnauthorizedError(Exception):
34+
class UnauthorizedError(ENSError):
2735
"""
2836
Raised if the sending account is not the owner of the name
2937
you are trying to modify. Make sure to set ``from`` in the
@@ -33,7 +41,7 @@ class UnauthorizedError(Exception):
3341
pass
3442

3543

36-
class UnownedName(Exception):
44+
class UnownedName(ENSError):
3745
"""
3846
Raised if you are trying to modify a name that no one owns.
3947
@@ -44,47 +52,47 @@ class UnownedName(Exception):
4452
pass
4553

4654

47-
class ResolverNotFound(Exception):
55+
class ResolverNotFound(ENSError):
4856
"""
4957
Raised if no resolver was found for the name you are trying to resolve.
5058
"""
5159

5260
pass
5361

5462

55-
class UnsupportedFunction(Exception):
63+
class UnsupportedFunction(ENSError):
5664
"""
5765
Raised if a resolver does not support a particular method.
5866
"""
5967

6068
pass
6169

6270

63-
class BidTooLow(ValueError):
71+
class BidTooLow(ENSError):
6472
"""
6573
Raised if you bid less than the minimum amount
6674
"""
6775

6876
pass
6977

7078

71-
class InvalidBidHash(ValueError):
79+
class InvalidBidHash(ENSError):
7280
"""
7381
Raised if you supply incorrect data to generate the bid hash.
7482
"""
7583

7684
pass
7785

7886

79-
class InvalidLabel(ValueError):
87+
class InvalidLabel(ENSError):
8088
"""
8189
Raised if you supply an invalid label
8290
"""
8391

8492
pass
8593

8694

87-
class OversizeTransaction(ValueError):
95+
class OversizeTransaction(ENSError):
8896
"""
8997
Raised if a transaction you are trying to create would cost so
9098
much gas that it could not fit in a block.
@@ -95,7 +103,7 @@ class OversizeTransaction(ValueError):
95103
pass
96104

97105

98-
class UnderfundedBid(ValueError):
106+
class UnderfundedBid(ENSError):
99107
"""
100108
Raised if you send less wei with your bid than you declared
101109
as your intent to bid.

newsfragments/1478.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All exceptions inherit from a custom class. EthPM exceptions inherit from PyEthPMError, ENS exceptions inherit from ENS Error, and all other web3.py exceptions inherit from Web3Exception

tests/core/eth-module/test_eth_contract.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
to_bytes,
88
)
99

10+
from web3.exceptions import (
11+
InvalidAddress,
12+
)
13+
1014
ABI = [{}]
1115
ADDRESS = "0xd3CdA913deB6f67967B99D67aCDFa1712C293601"
1216
BYTES_ADDRESS = to_bytes(hexstr=ADDRESS)
@@ -19,11 +23,11 @@
1923
(
2024
((ADDRESS,), {}, None),
2125
((BYTES_ADDRESS,), {}, None),
22-
((INVALID_CHECKSUM_ADDRESS,), {}, ValueError),
23-
((NON_CHECKSUM_ADDRESS,), {}, ValueError),
26+
((INVALID_CHECKSUM_ADDRESS,), {}, InvalidAddress),
27+
((NON_CHECKSUM_ADDRESS,), {}, InvalidAddress),
2428
((), {"address": ADDRESS}, None),
25-
((), {"address": INVALID_CHECKSUM_ADDRESS}, ValueError),
26-
((), {"address": NON_CHECKSUM_ADDRESS}, ValueError),
29+
((), {"address": INVALID_CHECKSUM_ADDRESS}, InvalidAddress),
30+
((), {"address": NON_CHECKSUM_ADDRESS}, InvalidAddress),
2731
),
2832
)
2933
def test_contract_address_validation(w3, args, kwargs, expected):

web3/exceptions.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@
1010
)
1111

1212

13-
class BadFunctionCallOutput(Exception):
13+
class Web3Exception(Exception):
14+
"""
15+
Exception mixin inherited by all exceptions of web3.py
16+
17+
This allows::
18+
19+
try:
20+
some_call()
21+
except Web3Exception:
22+
# deal with web3 exception
23+
except:
24+
# deal with other exceptions
25+
"""
26+
27+
28+
class BadFunctionCallOutput(Web3Exception):
1429
"""
1530
We failed to decode ABI output.
1631
@@ -20,15 +35,15 @@ class BadFunctionCallOutput(Exception):
2035
pass
2136

2237

23-
class BlockNumberOutofRange(Exception):
38+
class BlockNumberOutofRange(Web3Exception):
2439
"""
2540
block_identifier passed does not match known block.
2641
"""
2742

2843
pass
2944

3045

31-
class CannotHandleRequest(Exception):
46+
class CannotHandleRequest(Web3Exception):
3247
"""
3348
Raised by a provider to signal that it cannot handle an RPC request and
3449
that the manager should proceed to the next provider.
@@ -37,15 +52,15 @@ class CannotHandleRequest(Exception):
3752
pass
3853

3954

40-
class TooManyRequests(Exception):
55+
class TooManyRequests(Web3Exception):
4156
"""
4257
Raised by a provider to signal that too many requests have been made consecutively.
4358
"""
4459

4560
pass
4661

4762

48-
class MultipleFailedRequests(Exception):
63+
class MultipleFailedRequests(Web3Exception):
4964
"""
5065
Raised by a provider to signal that multiple requests to retrieve the same
5166
(or similar) data have failed.
@@ -54,15 +69,15 @@ class MultipleFailedRequests(Exception):
5469
pass
5570

5671

57-
class InvalidAddress(ValueError):
72+
class InvalidAddress(Web3Exception):
5873
"""
5974
The supplied address does not have a valid checksum, as defined in EIP-55
6075
"""
6176

6277
pass
6378

6479

65-
class NameNotFound(ValueError):
80+
class NameNotFound(Web3Exception):
6681
"""
6782
Raised when a caller provides an Ethereum Name Service name that
6883
does not resolve to an address.
@@ -71,7 +86,7 @@ class NameNotFound(ValueError):
7186
pass
7287

7388

74-
class StaleBlockchain(Exception):
89+
class StaleBlockchain(Web3Exception):
7590
"""
7691
Raised by the stalecheck_middleware when the latest block is too old.
7792
"""
@@ -93,7 +108,7 @@ def __str__(self) -> str:
93108
return self.args[0]
94109

95110

96-
class MismatchedABI(Exception):
111+
class MismatchedABI(Web3Exception):
97112
"""
98113
Raised when an ABI does not match with supplied parameters, or when an
99114
attempt is made to access a function/event that does not exist in the ABI.
@@ -120,15 +135,15 @@ class ABIFunctionNotFound(AttributeError, MismatchedABI):
120135
pass
121136

122137

123-
class FallbackNotFound(Exception):
138+
class FallbackNotFound(Web3Exception):
124139
"""
125140
Raised when fallback function doesn't exist in contract.
126141
"""
127142

128143
pass
129144

130145

131-
class ValidationError(Exception):
146+
class ValidationError(Web3Exception):
132147
"""
133148
Raised when a supplied value is invalid.
134149
"""
@@ -144,31 +159,31 @@ class ExtraDataLengthError(ValidationError):
144159
pass
145160

146161

147-
class NoABIFunctionsFound(AttributeError):
162+
class NoABIFunctionsFound(Web3Exception):
148163
"""
149164
Raised when an ABI is present, but doesn't contain any functions.
150165
"""
151166

152167
pass
153168

154169

155-
class NoABIFound(AttributeError):
170+
class NoABIFound(Web3Exception):
156171
"""
157172
Raised when no ABI is present.
158173
"""
159174

160175
pass
161176

162177

163-
class NoABIEventsFound(AttributeError):
178+
class NoABIEventsFound(Web3Exception):
164179
"""
165180
Raised when an ABI doesn't contain any events.
166181
"""
167182

168183
pass
169184

170185

171-
class InsufficientData(Exception):
186+
class InsufficientData(Web3Exception):
172187
"""
173188
Raised when there are insufficient data points to
174189
complete a calculation
@@ -177,7 +192,7 @@ class InsufficientData(Exception):
177192
pass
178193

179194

180-
class TimeExhausted(Exception):
195+
class TimeExhausted(Web3Exception):
181196
"""
182197
Raised when a method has not retrieved the desired
183198
result within a specified timeout.
@@ -186,7 +201,7 @@ class TimeExhausted(Exception):
186201
pass
187202

188203

189-
class PMError(Exception):
204+
class PMError(Web3Exception):
190205
"""
191206
Raised when an error occurs in the PM module.
192207
"""
@@ -202,48 +217,47 @@ class ManifestValidationError(PMError):
202217
pass
203218

204219

205-
class TransactionNotFound(Exception):
220+
class TransactionNotFound(Web3Exception):
206221
"""
207222
Raised when a tx hash used to lookup a tx in a jsonrpc call cannot be found.
208223
"""
209224

210225
pass
211226

212227

213-
class BlockNotFound(Exception):
228+
class BlockNotFound(Web3Exception):
214229
"""
215230
Raised when the block id used to lookup a block in a jsonrpc call cannot be found.
216231
"""
217232

218233
pass
219234

220235

221-
class InfuraProjectIdNotFound(Exception):
236+
class InfuraProjectIdNotFound(Web3Exception):
222237
"""
223238
Raised when there is no Infura Project Id set.
224239
"""
225240

226241
pass
227242

228243

229-
class LogTopicError(ValueError):
230-
# Inherits from ValueError for backwards compatibility
244+
class LogTopicError(Web3Exception):
231245
"""
232246
Raised when the number of log topics is mismatched.
233247
"""
248+
234249
pass
235250

236251

237-
class InvalidEventABI(ValueError):
238-
# Inherits from ValueError for backwards compatibility
252+
class InvalidEventABI(Web3Exception):
239253
"""
240254
Raised when the event ABI is invalid.
241255
"""
256+
242257
pass
243258

244259

245-
class ContractLogicError(ValueError):
246-
# Inherits from ValueError for backwards compatibility
260+
class ContractLogicError(Web3Exception):
247261
"""
248262
Raised on a contract revert error
249263
"""
@@ -259,7 +273,7 @@ def __init__(self, payload: Dict[str, Any]) -> None:
259273
super().__init__()
260274

261275

262-
class InvalidTransaction(Exception):
276+
class InvalidTransaction(Web3Exception):
263277
"""
264278
Raised when a transaction includes an invalid combination of arguments.
265279
"""
@@ -279,9 +293,9 @@ def __init__(self) -> None:
279293
super().__init__(message)
280294

281295

282-
class BadResponseFormat(ValueError, KeyError):
283-
# Inherits from KeyError and ValueError for backwards compatibility
296+
class BadResponseFormat(Web3Exception):
284297
"""
285298
Raised when a JSON-RPC response comes back in an unexpected format
286299
"""
300+
287301
pass

0 commit comments

Comments
 (0)