Im learning Solidity. As far as I'm aware, it's recommended using "require" to handle errors. But, I'd like to inform the users about the kind of errors they make, e.g. an argument is invalid or out of range, etc.

Question: How to write a error message in error handling functions like "require"?

Also, I need to know if it makes sense to have such error messages in contract?

  • 1
    Suggest you accept an answer so you can gain reputation and reward the helpful answers.
    – Cyberience
    Commented May 10, 2019 at 5:06

6 Answers 6


It is included since Solidity 0.4.22. There is a merged branch adding this feature in the milestone.

The documentation states

There are two other ways to trigger exceptions: The revert function can be used to flag an error and revert the current call. In the future it might be possible to also include details about the error in a call to revert. The throw keyword can also be used as an alternative to revert().

In the past it was used like this

    msg.sender == _account

And in Solidity 0.4.22 and newer it can be used like this

    msg.sender == _account,
    "Sender not authorized."

Adding the reason as the second parameter

  • 17
    Where do you view this though?
    – JohnAllen
    Commented Sep 25, 2018 at 20:35

You can use Events to log error messages. There is nothing like the print statement in solidity, so you can't directly tell users about the error messages. However, Events works similar to logs in other languages, which helps to log such cases and notify errors.

In one of my project, we chose to have status codes for the different type of errors and then every time in failure cases, we logged those status codes with the help of events.

You can see an implementation here at this repo -

Contract code - https://github.com/Imaginea/lms/blob/master/contracts/MembersLibrary.sol#L27

Note: These status codes can be accessed as well from the response, as shown here -

https://github.com/Imaginea/lms/blob/master/test/testOrganisation.js#L45 (Test Case)

Hope this helps.

  • Thanks for the answer! I noticed that in your code you get floor of a fraction (floating) value. Can you tell me how to do it in Solidity.
    – Aydin
    Commented Aug 23, 2017 at 11:49
  • I've not done it, will have to find out. I'll come back to you on this.
    – Sanchit
    Commented Aug 23, 2017 at 11:50
  • @Sanchit Couldn't you run into a problem where you can't generate the error Event because you don't have enough gas? I believe that the stipend is not enough to cover the generation of an event, if I've got that right. Commented Jun 21, 2018 at 22:26
  • Unfortunately, both these links no longer work. Commented Nov 17, 2021 at 17:07
  • Imaginea was acquired by another company hence all links under their Github org are gone. Please access the links using my fork - github.com/inovizz/lms
    – Sanchit
    Commented Jan 18, 2022 at 12:06

As of November 5 2017, it's not yet possible.

Keep an eye on:

https://github.com/ethereum/solidity/issues/1686 Support reason string in revert (and perhaps require / assert)

When it is implemented, syntax like require(x>0, "positive number needed"); will probably work.


The best way to get an error message back is to trigger an event based on the result or return a string. The usual reason for using require versus assert is that if require fails, it will refund the remaining gas and revert all changes. On the other hand, assert will use up all the remaining gas before reverting all changes. A small edit to add, is the recommendation of adding a rejection message to the require, it is optional as of version 0.5.8, but the compilers and jet-brains flags up a warning if you don't, and it may become mandatory in the future, The syntax is the same though.

assert(myAmount < __someValue);

require(myAmount < __someValue,"Insufficient funds to allow transfer");

And this is pretty good description to help you along: How to use Assert and require

  • Thanks for the Edit @savard, I am a Dyslexic, and sometimes its tough, hit the send before looking for red underlines. I would add that on the require, its now recommended to place a text rejection message as the last parameter to the require! require(myAmount < __someValue,"Amount must be less than somevalue");
    – Cyberience
    Commented Apr 29, 2019 at 3:13
  • No worries @Cyberience, completely understandable. I'm not dyslexic but I still have a tough time with grammar too. Also, that would be a great edit to add regarding the ability to add an error message to require() statement - very helpful in development.
    – savard
    Commented Apr 29, 2019 at 5:23

Error reason strings for revert and require has been introduced. For more details: https://medium.com/secureblocks/solidity-0-4-22-enhancements-94d2b9b8b6fe

  • I guess it is still not possible to catch these on client side like web3.js (github.com/ethereum/web3.js/issues/1707) Commented Jun 29, 2018 at 14:29
  • 1
    Yes. But remix started to show the reason. Hope soon it will be available for web3 too.
    – Aniket
    Commented Jun 30, 2018 at 18:57

I was compiling my code with solidity ^0.4.24 and I had to remove error messages from require() as when I tried to deploy my contract with web3.eth.Contract().deploy.send() I would get a 'The contract code couldn't be stored, please check your gas limit.' error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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