@@ -550,13 +550,30 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
550
550
else :
551
551
raise AssertionError ("No exception raised" )
552
552
553
- def assert_raises_jsonrpc (code , fun , * args , ** kwds ):
554
- '''Check for specific JSONRPC exception code'''
553
+ def assert_raises_jsonrpc (code , message , fun , * args , ** kwds ):
554
+ """Run an RPC and verify that a specific JSONRPC exception code and message is raised.
555
+
556
+ Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException
557
+ and verifies that the error code and message are as expected. Throws AssertionError if
558
+ no JSONRPCException was returned or if the error code/message are not as expected.
559
+
560
+ Args:
561
+ code (int), optional: the error code returned by the RPC call (defined
562
+ in src/rpc/protocol.h). Set to None if checking the error code is not required.
563
+ message (string), optional: [a substring of] the error string returned by the
564
+ RPC call. Set to None if checking the error string is not required
565
+ fun (function): the function to call. This should be the name of an RPC.
566
+ args*: positional arguments for the function.
567
+ kwds**: named arguments for the function.
568
+ """
555
569
try :
556
570
fun (* args , ** kwds )
557
571
except JSONRPCException as e :
558
- if e .error ["code" ] != code :
572
+ # JSONRPCException was thrown as expected. Check the code and message values are correct.
573
+ if (code is not None ) and (code != e .error ["code" ]):
559
574
raise AssertionError ("Unexpected JSONRPC error code %i" % e .error ["code" ])
575
+ if (message is not None ) and (message not in e .error ['message' ]):
576
+ raise AssertionError ("Expected substring not found:" + e .error ['message' ])
560
577
except Exception as e :
561
578
raise AssertionError ("Unexpected exception raised: " + type (e ).__name__ )
562
579
else :
0 commit comments