I tried using require method in solidity in the following way:

require(validate(personName),"Error: Invalid Person")

In this case i am checking if a person is already added to my person list. If not then display the error message in console "Invalid Person". Now, my problem is when I run the function with valid name I get no error. But incase of invalid person name, it displays:

Error: VM Exception while processing transaction: revert
    at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
    at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
    at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
    at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:

instead of showing "Error: Invalid Person". Can any one tell me what I am doing wrong here.


You misunderstand what revert() does.

It will throw an exception if the expression evaluates to false.

The possibility of adding an explanation has been bubbling up through the stack since the Byzantium release. This is supported at the EVM level and the solc compiler level, so you can successfully include the explanation for the error in your contracts.

Hope it helps.

  • so what should i be using to show error message to the user( i am using truffle console currently. i have not connected my contracts to the front end) – aib92 Sep 17 '18 at 18:16
  • Client side, you can .catch(... the exception and infer the reason os simply handle as a failure. Have a look at Shawn's idea about Truffle 5 beta. Better options are in the pipeline. – Rob Hitchens Sep 17 '18 at 18:20

Support for "reason strings" for revert errors is available in Truffle version 5 which is currently in beta.

Revert with reason strings!! Find out the reason.

Using this, you should be able to write tests which check for the specific reason why a function reverted, rather than capturing a general error.

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.