Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwing more specific Exception on failure to retrieve result. #166

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ function-naming-style=snake_case
good-names=i,
j,
k,
e,
x,
m,
ex,
Run,
_
Expand Down Expand Up @@ -557,7 +560,7 @@ valid-metaclass-classmethod-first-arg=cls
[DESIGN]

# Maximum number of arguments for function / method.
max-args=5
max-args=7

# Maximum number of attributes for a class (see R0902).
max-attributes=7
Expand All @@ -566,7 +569,7 @@ max-attributes=7
max-bool-expr=5

# Maximum number of branch for function / method body.
max-branches=12
max-branches=25

# Maximum number of locals for function / method body.
max-locals=15
Expand All @@ -581,7 +584,7 @@ max-public-methods=20
max-returns=6

# Maximum number of statements in function / method body.
max-statements=50
max-statements=70

# Minimum number of public methods for a class (see R0903).
min-public-methods=2
Expand Down
14 changes: 9 additions & 5 deletions src/solana/rpc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class RPCException(Exception):
"""Raised when RPC method returns an error result."""


class RPCNoResultException(Exception):
"""Raised when an RPC method returns no result."""


class UnconfirmedTxError(Exception):
"""Raise when confirming a transaction times out."""

Expand Down Expand Up @@ -400,17 +404,17 @@ def _set_log_filter_args(log_filter: str) -> Tuple[types.RPCMethod, str]:

@staticmethod
def _post_send(resp: types.RPCResponse) -> types.RPCResponse:
maybe_error = resp.get("error")
if maybe_error is not None:
raise RPCException(maybe_error)
error = resp.get("error")
if error:
raise RPCException(error)
if not resp.get("result"):
raise Exception("Failed to send transaction")
raise RPCNoResultException("Failed to send transaction")
return resp

@staticmethod
def parse_recent_blockhash(blockhash_resp: types.RPCResponse) -> Blockhash:
"""Extract blockhash from JSON RPC result."""
if not blockhash_resp["result"]:
if not blockhash_resp.get("result"):
raise RuntimeError("failed to get recent blockhash")
return Blockhash(blockhash_resp["result"]["value"]["blockhash"])

Expand Down