Skip to content

Commit 881f5ae

Browse files
authored
Add data info to RPC error message (#1244)
1 parent 74249a6 commit 881f5ae

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

starknet_py/net/client_errors.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ class ClientError(Exception):
99
Base class for all errors raised while attempting to communicate with Starknet through Client.
1010
"""
1111

12-
def __init__(self, message: str, code: Optional[str] = None):
12+
def __init__(
13+
self, message: str, code: Optional[str] = None, data: Optional[str] = None
14+
):
1315
self.code = code
14-
self.message = f"Client failed{f' with code {code}' if code is not None else ''}: {message}."
16+
self.data = data
17+
self.message = (
18+
f"Client failed{f' with code {code}' if code is not None else ''}. "
19+
f"Message: {message}.{f' Data: {data}' if data is not None else ''}"
20+
)
21+
1522
super().__init__(self.message)
1623

1724

starknet_py/net/http_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def handle_rpc_error(result: dict):
8080
if "error" not in result:
8181
raise ServerError(body=result)
8282
raise ClientError(
83-
code=result["error"]["code"], message=result["error"]["message"]
83+
code=result["error"]["code"],
84+
message=result["error"]["message"],
85+
data=result["error"].get("data"),
8486
)
8587

8688
async def handle_request_error(self, request: ClientResponse):

starknet_py/tests/e2e/client/full_node_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def test_get_class_at(
8686
@pytest.mark.asyncio
8787
async def test_get_class_at_throws_on_wrong_address(client):
8888
with pytest.raises(
89-
ClientError, match="Client failed with code 20: Contract not found."
89+
ClientError, match="Client failed with code 20. Message: Contract not found."
9090
):
9191
await client.get_class_at(contract_address=0, block_hash="latest")
9292

starknet_py/tests/e2e/tests_on_networks/client_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ async def test_transaction_not_received_invalid_nonce(full_node_account_testnet)
130130
await account.client.send_transaction(sign_invoke)
131131

132132

133-
# TODO (#1219): fix this error once ClientError returns data field
134-
@pytest.mark.skip
135133
@pytest.mark.asyncio
136134
async def test_transaction_not_received_invalid_signature(full_node_account_testnet):
137135
account = full_node_account_testnet
@@ -145,9 +143,12 @@ async def test_transaction_not_received_invalid_signature(full_node_account_test
145143
)
146144
sign_invoke = dataclasses.replace(sign_invoke, signature=[0x21, 0x37])
147145

148-
with pytest.raises(ClientError, match=r"(.*Signature.*)|(.*An unexpected error.*)"):
146+
with pytest.raises(ClientError, match=r"Signature.*is invalid") as exc:
149147
await account.client.send_transaction(sign_invoke)
150148

149+
assert exc.value.data is not None
150+
assert "Data:" in exc.value.message
151+
151152

152153
# ------------------------------------ FULL_NODE_CLIENT TESTS ------------------------------------
153154

0 commit comments

Comments
 (0)