[Ethereum] Solidity: How to write a error message in “require”

contract-designsolidity

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?

Best Answer

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

require(
    msg.sender == _account
);

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

require(
    msg.sender == _account,
    "Sender not authorized."
);

Adding the reason as the second parameter