Does anyone know if there is way to pass helpful error messages from a contract to web3? For example:

require(value > balance); // would be great if second arg was message

When testing with truffle I only get VM Exception while processing transaction: revert

I guess something I could do is to pass a uid to my function from the client thats created right before the call and emit an event with helpful messages and match uid's? for example:

event ErrorMessage(bytes32 uid, string msg);  

function doSomething (uint value, bytes32 uid) {
    if(value > balance) {
        ErrorMessage(uid, 'SomeErrorCode');
  • I can't seem to find a good link now, but I believe a parameter for revert (and second parameter for require) are being discussed. I think it's likely this will get implemented, but I have no idea when.
    – user19510
    Mar 5 '18 at 2:50
  • Emitting an event won't have any effect if the transaction gets reverted. If you want to do something like this, you have to not revert the transaction.
    – user19510
    Mar 5 '18 at 2:50
  • 1
    @smarx well I definitely want to revert. Maybe I will just stick to require and handle general errors
    – pizzarob
    Mar 5 '18 at 3:12
  • I think that's the best option for right now.
    – user19510
    Mar 5 '18 at 3:14

Since solidity 0.4.22 you can put an error message in the require as a second argument. https://github.com/ethereum/solidity/releases/tag/v0.4.22

pragma solidity ^0.4.23;

contract Test {
        bytes32 name; // to check consumed gas

        function go() public {
            name = 'this is a test';
            require(false, "Error in script");
            name = 'another test';

  • 1
    Use 0.4.23, it has some bug fixes of 0.4.22.
    – Aniket
    May 3 '18 at 11:42
  • 3
    You can't get the error in web3 as of now. May 3 '18 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.