Skip to content

Commit c4795ac

Browse files
authored
Raise a RpcError rather than a ValueError (#54)
1 parent 9b20c4d commit c4795ac

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/cryptoadvance/spectrum/elsock.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010
from queue import Queue
1111

12+
from .spectrum_error import RPCError
1213
from .util import FlaskThread, SpectrumInternalException, handle_exception
1314

1415
# TODO: normal handling of ctrl+C interrupt
@@ -559,7 +560,10 @@ def call(self, method, params=[]) -> dict:
559560
)
560561
res = self._results.pop(uid)
561562
if "error" in res:
562-
raise ValueError(res["error"])
563+
if "code" in res["error"] and "message" in res["error"]:
564+
raise RPCError(res["error"]["message"], res["error"]["code"])
565+
else:
566+
raise SpectrumInternalException(res)
563567
if "result" in res:
564568
return res["result"]
565569

src/cryptoadvance/spectrum/spectrum.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from embit.transaction import TransactionInput, TransactionOutput
2424
from sqlalchemy.sql import func
2525

26+
from .spectrum_error import RPCError
2627
from .db import UTXO, Descriptor, Script, Tx, TxCategory, Wallet, db
2728
from .elsock import ElectrumSocket, ElSockTimeoutException
2829
from .util import (
@@ -36,6 +37,7 @@
3637
scripthash,
3738
)
3839

40+
3941
logger = logging.getLogger(__name__)
4042

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

7072

71-
class RPCError(Exception):
72-
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""
73-
74-
def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
75-
self.message = message
76-
self.code = code
77-
78-
def to_dict(self):
79-
return {"code": self.code, "message": self.message}
80-
81-
8273
# we detect chain by looking at the hash of the 0th block
8374
ROOT_HASHES = {
8475
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f": "main",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class RPCError(Exception):
2+
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""
3+
4+
def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
5+
self.message = message
6+
self.code = code
7+
8+
def to_dict(self):
9+
return {"code": self.code, "message": self.message}

0 commit comments

Comments
 (0)