0

I tried to run the following code to check the error message when the condition of require() is not met.

pragma solidity ^0.5.16;
...
function addUser(string memory name) public returns (int) {
   require(!userList[msg.sender].isSigned, "You already have an account.");
   ...
}

When the condition of require() is met, it worked as expected, but when the condition wasn't met, I could always see the following error message:

transact to Messenger.addUser errored: gas required exceeds allowance (8946128) or always failing transaction

I thought that the problem was due to the gas limit of the block, so I increased the gas limit from ten thousand to one million. However, the problem was not solved and I don't know what the cause is.

What I want to ask is

  • Does solidity always show that error message when the condition of require() is not met?
  • How to check the message("You already have an account.") I specified in require()?

Thanks for help :)

  • "How to check the message("You already have an account.") I specified in require()?" - what does that mean? – goodvibration Jan 23 at 6:52
  • I thought the second parameter of require() would be the error message, and it would be printed when the condition of require () was not met. Is it right? I want to see that message printed out. (In my case, "You already have an account.") – W.J Lee Jan 23 at 7:07
  • It is right actually. At least that's the case in Truffle 4.x, which relies on Web3 0.x (so I'm not sure which one of those two is in charge of this feature). Your error-message is from Web3 v1.x as far as I'm aware of; not really sure how you could get your require-message on that one. – goodvibration Jan 23 at 9:06
1

Does solidity always show that error message when the condition of require is not met?

First of all, it is not Solidity which generates this error-message, it is Web3.js.

Second, it is indeed arguably not the most suitable error-message because it makes one think that it has something to do with the gas, where in fact the transaction reverts because the condition of the require is not met. But if you take a good look at that message, then you'll see "or always failing transaction", which aligns perfectly with the reason of the failure.

|improve this answer|||||
  • I'd previously run the smart contract using Web3 Provider in remix. But using Javascript VM, I could see the message I set. Your advice was helpful. Thank you :) – W.J Lee Jan 23 at 16:19
  • @W.JLee: You're welcome :) – goodvibration Jan 23 at 17:21

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.