Tries to run an rpc command. Test against error code and message if the rpc fails. Returns whether a JSONRPCException was raised.
(code, message, fun, *args, **kwds)
| 172 | |
| 173 | |
| 174 | def try_rpc(code, message, fun, *args, **kwds): |
| 175 | """Tries to run an rpc command. |
| 176 | |
| 177 | Test against error code and message if the rpc fails. |
| 178 | Returns whether a JSONRPCException was raised.""" |
| 179 | try: |
| 180 | fun(*args, **kwds) |
| 181 | except JSONRPCException as e: |
| 182 | # JSONRPCException was thrown as expected. Check the message and code values are correct. |
| 183 | if (message is not None) and (message not in e.error['message']): |
| 184 | raise AssertionError( |
| 185 | "Expected substring not found in error message:\nsubstring: '{}'\nerror message: '{}'.".format( |
| 186 | message, e.error['message'])) |
| 187 | if (code is not None) and (code != e.error["code"]): |
| 188 | raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"]) |
| 189 | return True |
| 190 | except Exception as e: |
| 191 | raise AssertionError("Unexpected exception raised: " + type(e).__name__) |
| 192 | else: |
| 193 | return False |
| 194 | |
| 195 | |
| 196 | def assert_is_hex_string(string): |