|
| 1 | +import pytest |
| 2 | + |
| 3 | +from web3._utils.method_formatters import ( |
| 4 | + get_error_formatters, |
| 5 | + raise_solidity_error_on_revert, |
| 6 | +) |
| 7 | +from web3._utils.rpc_abi import ( |
| 8 | + RPC, |
| 9 | +) |
| 10 | +from web3.exceptions import ( |
| 11 | + SolidityError, |
| 12 | +) |
| 13 | +from web3.types import ( |
| 14 | + RPCResponse, |
| 15 | +) |
| 16 | + |
| 17 | +REVERT_WITH_MSG = RPCResponse({ |
| 18 | + 'jsonrpc': '2.0', |
| 19 | + 'error': { |
| 20 | + 'code': -32015, |
| 21 | + 'message': 'VM execution error.', |
| 22 | + 'data': ( |
| 23 | + 'Reverted ' |
| 24 | + '0x08c379a' |
| 25 | + '00000000000000000000000000000000000000000000000000000000000000020' |
| 26 | + '0000000000000000000000000000000000000000000000000000000000000016' |
| 27 | + '6e6f7420616c6c6f77656420746f206d6f6e69746f7200000000000000000000' |
| 28 | + ), |
| 29 | + }, |
| 30 | + 'id': 2987, |
| 31 | +}) |
| 32 | + |
| 33 | +REVERT_WITHOUT_MSG = RPCResponse({ |
| 34 | + 'jsonrpc': '2.0', |
| 35 | + 'error': { |
| 36 | + 'code': -32015, |
| 37 | + 'message': 'VM execution error.', |
| 38 | + 'data': 'Reverted 0x', |
| 39 | + }, |
| 40 | + 'id': 2987, |
| 41 | +}) |
| 42 | + |
| 43 | +OTHER_ERROR = RPCResponse({ |
| 44 | + "jsonrpc": "2.0", |
| 45 | + "error": { |
| 46 | + "code": -32601, |
| 47 | + "message": "Method not found", |
| 48 | + }, |
| 49 | + "id": 1, |
| 50 | +}) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "response,expected", |
| 55 | + ( |
| 56 | + (REVERT_WITH_MSG, 'execution reverted: not allowed to monitor'), |
| 57 | + (REVERT_WITHOUT_MSG, 'execution reverted'), |
| 58 | + ), |
| 59 | + ids=[ |
| 60 | + 'test-get-revert-reason-with-msg', |
| 61 | + 'test-get-revert-reason-without-msg', |
| 62 | + ]) |
| 63 | +def test_get_revert_reason(response, expected) -> None: |
| 64 | + with pytest.raises(SolidityError, match=expected): |
| 65 | + raise_solidity_error_on_revert(response) |
| 66 | + |
| 67 | + |
| 68 | +def test_get_revert_reason_other_error() -> None: |
| 69 | + assert raise_solidity_error_on_revert(OTHER_ERROR) is OTHER_ERROR |
| 70 | + |
| 71 | + |
| 72 | +def test_get_error_formatters() -> None: |
| 73 | + formatters = get_error_formatters(RPC.eth_call) |
| 74 | + with pytest.raises(SolidityError, match='not allowed to monitor'): |
| 75 | + formatters(REVERT_WITH_MSG) |
| 76 | + with pytest.raises(SolidityError): |
| 77 | + formatters(REVERT_WITHOUT_MSG) |
| 78 | + assert formatters(OTHER_ERROR) == OTHER_ERROR |
0 commit comments