Skip to content

Commit 1b39b44

Browse files
committed
Review exceptions returned by ClientSession[RPC]
1 parent 01a0798 commit 1b39b44

5 files changed

Lines changed: 74 additions & 27 deletions

File tree

docs/api.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Clients
1313
.. autoclass:: ClientSession()
1414
:members:
1515

16+
.. autoclass:: ClientSessionRPC()
17+
:members:
18+
1619

1720
Providers
1821
---------
@@ -46,14 +49,22 @@ Errors
4649
------
4750

4851
.. autoclass:: pons.ABIDecodingError
49-
50-
.. autoclass:: pons.RemoteError
52+
:show-inheritance:
5153

5254
.. autoclass:: pons.Unreachable
55+
:show-inheritance:
5356

5457
.. autoclass:: pons.ProtocolError
58+
:show-inheritance:
59+
60+
.. autoclass:: pons.HTTPError
61+
:show-inheritance:
5562

5663
.. autoclass:: pons.TransactionFailed
64+
:show-inheritance:
65+
66+
.. autoclass:: pons.BadResponseFormat
67+
:show-inheritance:
5768

5869
.. autoclass:: pons.ProviderError()
5970
:show-inheritance:

pons/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
ContractError,
99
ContractLegacyError,
1010
ContractPanic,
11-
ProviderError,
12-
RemoteError,
1311
TransactionFailed,
1412
)
13+
from ._client_rpc import BadResponseFormat, ClientSessionRPC, ProviderError
1514
from ._compiler import EVMVersion, compile_contract_file
1615
from ._contract import (
1716
BaseBoundMethodCall,
@@ -49,14 +48,15 @@
4948
from ._http_provider_server import HTTPProviderServer
5049
from ._local_provider import LocalProvider, SnapshotID
5150
from ._multicall import BoundMultiMethodCall, BoundMultiMethodValueCall, Multicall
52-
from ._provider import HTTPProvider, ProtocolError, Provider, Unreachable
51+
from ._provider import HTTPError, HTTPProvider, ProtocolError, Provider, Unreachable
5352
from ._signer import AccountSigner, Signer
5453
from ._utils import get_create2_address, get_create_address
5554

5655
__all__ = [
5756
"ABI_JSON",
5857
"ABIDecodingError",
5958
"AccountSigner",
59+
"BadResponseFormat",
6060
"BaseBoundMethodCall",
6161
"BoundConstructor",
6262
"BoundConstructorCall",
@@ -68,6 +68,7 @@
6868
"BoundMultiMethodValueCall",
6969
"Client",
7070
"ClientSession",
71+
"ClientSessionRPC",
7172
"CompiledContract",
7273
"Constructor",
7374
"ConstructorCall",
@@ -86,6 +87,7 @@
8687
"FallbackProvider",
8788
"FallbackStrategy",
8889
"FallbackStrategyFactory",
90+
"HTTPError",
8991
"HTTPProvider",
9092
"HTTPProviderServer",
9193
"LocalProvider",
@@ -99,7 +101,6 @@
99101
"Provider",
100102
"ProviderError",
101103
"Receive",
102-
"RemoteError",
103104
"Signer",
104105
"SnapshotID",
105106
"TransactionFailed",

pons/_client.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
unstructure,
2222
)
2323

24-
from ._client_rpc import BadResponseFormat, ClientSessionRPC, ProviderError, RemoteError
24+
from ._client_rpc import BadResponseFormat, ClientSessionRPC, ProviderError
2525
from ._contract import (
2626
BaseBoundMethodCall,
2727
BoundConstructorCall,
@@ -61,10 +61,10 @@ async def session(self) -> AsyncIterator["ClientSession"]:
6161
# TODO (#58): incorporate cached values from the session back into the client
6262

6363

64-
class TransactionFailed(RemoteError):
64+
class TransactionFailed(Exception):
6565
"""
66-
Raised if the transaction was submitted successfully,
67-
but the final receipt indicates a failure.
66+
Raised for invalid transactions that are not contract executions
67+
(e.g. transfers or contract deployments).
6868
"""
6969

7070

@@ -121,7 +121,7 @@ def from_int(cls, val: int) -> "ContractPanicReason":
121121
return cls.UNKNOWN
122122

123123

124-
class ContractPanic(RemoteError):
124+
class ContractPanic(Exception):
125125
"""A panic raised in a contract call."""
126126

127127
Reason = ContractPanicReason
@@ -138,7 +138,7 @@ def __init__(self, reason: ContractPanicReason):
138138
self.reason = reason
139139

140140

141-
class ContractLegacyError(RemoteError):
141+
class ContractLegacyError(Exception):
142142
"""A raised Solidity legacy error (from ``require()`` or ``revert()``)."""
143143

144144
message: str
@@ -149,7 +149,7 @@ def __init__(self, message: str):
149149
self.message = message
150150

151151

152-
class ContractError(RemoteError):
152+
class ContractError(Exception):
153153
"""A raised Solidity error (from ``revert SomeError(...)``)."""
154154

155155
error: Error
@@ -186,7 +186,19 @@ def decode_contract_error(
186186

187187

188188
class ClientSession:
189-
"""An open session to the provider."""
189+
"""
190+
An open session to the provider.
191+
192+
The methods of this class may raise the following exceptions:
193+
:py:class:`ProviderError`,
194+
:py:class:`ContractLegacyError`,
195+
:py:class:`ContractError`,
196+
:py:class:`ContractPanic`,
197+
:py:class:`TransactionFailed`,
198+
:py:class:`Unreachable`,
199+
:py:class:`BadResponseFormat`,
200+
a provider-specific derived class of :py:class:`ProtocolError`.
201+
"""
190202

191203
def __init__(self, provider_session: ProviderSession):
192204
self._provider_session = provider_session

pons/_client_rpc.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,17 @@ class LogFilter:
4747
provider_path: tuple[int, ...]
4848

4949

50-
class RemoteError(Exception):
51-
"""
52-
A base of all errors occurring on the provider's side.
53-
Encompasses both errors returned via HTTP status codes
54-
and the ones returned via the JSON response.
55-
"""
56-
57-
58-
class BadResponseFormat(RemoteError):
50+
class BadResponseFormat(Exception):
5951
"""Raised if the RPC provider returned an unexpectedly formatted response."""
6052

6153

62-
class ProviderError(RemoteError):
63-
"""A general problem with fulfilling the request at the provider's side."""
54+
class ProviderError(Exception):
55+
"""
56+
A general problem with fulfilling the request at the provider's side.
57+
58+
This means the provider sent a correct response with an error code
59+
and possibly some associated data.
60+
"""
6461

6562
raw_code: int
6663
"""The error code returned by the server."""
@@ -144,6 +141,16 @@ async def rpc_call_at_pin(
144141

145142

146143
class ClientSessionRPC:
144+
"""
145+
The hub for methods which directly correspond to Ethereum RPC calls.
146+
147+
The methods of this class may raise the following exceptions:
148+
:py:class:`ProviderError`,
149+
:py:class:`Unreachable`,
150+
:py:class:`BadResponseFormat`,
151+
a provider-specific derived class of :py:class:`ProtocolError`.
152+
"""
153+
147154
def __init__(self, provider_session: ProviderSession):
148155
self._provider_session = provider_session
149156

pons/_provider.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,27 @@ class Unreachable(Exception):
2121
"""Raised when there is a problem connecting to the provider."""
2222

2323

24-
class ProtocolError(Exception):
25-
"""A protocol-specific error."""
24+
class ProtocolError(ABC, Exception):
25+
"""
26+
A protocol-specific error, indicating that the provider returned an error status
27+
with no additional information allowing to categorize the error further.
28+
29+
See the provider-specifc derived class for this exception for more details.
30+
"""
2631

2732

2833
class HTTPError(ProtocolError):
34+
"""
35+
Raised when the provider returns a response with a status code other than 200,
36+
and no ``"error"`` field in the associated JSON data.
37+
"""
38+
39+
status: HTTPStatus
40+
"""The HTTP status of the response."""
41+
42+
message: str
43+
"""The response body."""
44+
2945
def __init__(self, status_code: int, message: str):
3046
try:
3147
status = HTTPStatus(status_code)

0 commit comments

Comments
 (0)