Skip to content

Commit

Permalink
Raise a RpcError rather than a ValueError (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
k9ert authored May 15, 2024
1 parent 9b20c4d commit c4795ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/cryptoadvance/spectrum/elsock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
from queue import Queue

from .spectrum_error import RPCError
from .util import FlaskThread, SpectrumInternalException, handle_exception

# TODO: normal handling of ctrl+C interrupt
Expand Down Expand Up @@ -559,7 +560,10 @@ def call(self, method, params=[]) -> dict:
)
res = self._results.pop(uid)
if "error" in res:
raise ValueError(res["error"])
if "code" in res["error"] and "message" in res["error"]:
raise RPCError(res["error"]["message"], res["error"]["code"])
else:
raise SpectrumInternalException(res)
if "result" in res:
return res["result"]

Expand Down
13 changes: 2 additions & 11 deletions src/cryptoadvance/spectrum/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from embit.transaction import TransactionInput, TransactionOutput
from sqlalchemy.sql import func

from .spectrum_error import RPCError
from .db import UTXO, Descriptor, Script, Tx, TxCategory, Wallet, db
from .elsock import ElectrumSocket, ElSockTimeoutException
from .util import (
Expand All @@ -36,6 +37,7 @@
scripthash,
)


logger = logging.getLogger(__name__)

# a set of registered rpc calls that do not need a wallet
Expand Down Expand Up @@ -68,17 +70,6 @@ def wrapper(*args, **kwargs):
return wrapper


class RPCError(Exception):
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""

def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
self.message = message
self.code = code

def to_dict(self):
return {"code": self.code, "message": self.message}


# we detect chain by looking at the hash of the 0th block
ROOT_HASHES = {
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f": "main",
Expand Down
9 changes: 9 additions & 0 deletions src/cryptoadvance/spectrum/spectrum_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RPCError(Exception):
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""

def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
self.message = message
self.code = code

def to_dict(self):
return {"code": self.code, "message": self.message}

0 comments on commit c4795ac

Please sign in to comment.