solidity – How to Return Error Message from Smart Contract

ethereumjsquorumremixsolidityweb3js

I am trying to return error messages from solidity. I have this contract:

pragma solidity ^0.4.23;
contract Simple {
    function arithmetics(uint a, uint b){           
                     require(a % 2 == 0, "Even value required.");             
                    b = b * 3; 
            }
}

When I test in remix, it's returning me the following error message:

transact to Simple.arithmetics errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Reason provided by the contract: "Even value required.". Debug the transaction to get more information.

However, when I tried calling it from my dApp using web3.js I am not getting an error message, only this:

Transaction ran out of gas.

Best Answer

You can find the error message tracing the transaction. For example, using geth and I assume your node is running at http://127.0.0.1:8545:

1- Open a console

geth attach http://127.0.0.1:8545

2- Trace transaction

debug.traceTransaction(<your TX hash here>)

You should get a transaction trace. If your geth node wasn't started with debug enabled, you can just do an HTTP request like:

curl -X POST --data '{"jsonrpc":"2.0","method":"debug_traceTransaction","params":["<your TX hash here>"],"id":1}' http://127.0.0.1

Now that you have the trace, you can prettify the JSON to see it better. At the very bottom you will find the last instruction and the EVM state (and other things). The last instruction should be a REVERT opcode. Then, go to the MEMORY section, and you can convert it to ascii to find the error message. For example, in bash this command would convert from hex to ascii:

echo <your HEX here> | xxd -r -p

Related Topic