Skip to content

Commit fcfb952

Browse files
ryanofskyjnewbery
authored andcommitted
Improve TestNodeCLI output parsing
Parse JSONRPCException errors, and avoid JSON decode exception if RPC method returns a plain string.
1 parent 45173fa commit fcfb952

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

test/functional/test_framework/test_node.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import json
1111
import logging
1212
import os
13+
import re
1314
import subprocess
1415
import time
1516

@@ -22,6 +23,9 @@
2223
p2p_port,
2324
)
2425

26+
# For Python 3.4 compatibility
27+
JSONDecodeError = getattr(json, "JSONDecodeError", ValueError)
28+
2529
BITCOIND_PROC_WAIT_TIMEOUT = 60
2630

2731
class TestNode():
@@ -222,6 +226,13 @@ def send_cli(self, command, *args, **kwargs):
222226
cli_stdout, cli_stderr = process.communicate(input=self.input)
223227
returncode = process.poll()
224228
if returncode:
229+
match = re.match(r'error code: ([-0-9]+)\nerror message:\n(.*)', cli_stderr)
230+
if match:
231+
code, message = match.groups()
232+
raise JSONRPCException(dict(code=int(code), message=message))
225233
# Ignore cli_stdout, raise with cli_stderr
226234
raise subprocess.CalledProcessError(returncode, self.binary, output=cli_stderr)
227-
return json.loads(cli_stdout, parse_float=decimal.Decimal)
235+
try:
236+
return json.loads(cli_stdout, parse_float=decimal.Decimal)
237+
except JSONDecodeError:
238+
return cli_stdout.rstrip("\n")

0 commit comments

Comments
 (0)