Published on

The Importance of Error Messages in Solidity Requires

Authors

In Solidity you can verify certain conditions and make your contract fail and refund gas if you use the require.

Using the require expression is fairly simple:

require(someBooleanConditionToCheck, "Optional error message if condition fails");

An example of this would be if you do not want to allow someone's age to be less than 18:

require(age >= 18, "A bar patron cannot be younger than 18");

In the above if the age provided is less than 18 the transaction will be rejected with an error message of the form:

 "Error: Error: [ethjs-rpc] rpc error with payload {…VM Exception while processing transaction: revert A bar patron cannot be younger than 18"

This error message is optional in the require but as I found today adding the error message is very important in assisting with debugging. For example in the above if we made the require:

require(age >= 18);

And now pass an age that is invalid we get:

 "Error: Error: [ethjs-rpc] rpc error with payload {…VM Exception while processing transaction: revert"

The issue here is that we cannot tell what caused this error. This error is also the fallback if basically anything goes wrong in your solidity code.

While require error messages are not mandatory it is clear that adding them is very important as they assist greatly in debugging issues with your contract code. You could even add a random error code to the message string so that you can easily identify what require failed in your code.