Custom Errors in Solidity: A More Efficient and More Expressive Approach
Simone Romano
Posted on August 6, 2023
One of the coolest features introduced in Solidity version 0.8.4 is "Custom Errors", and I love it. Custom Errors replace the traditional "revert with message" approach, giving us a more efficient and more expressive way to handle errors in our smart contracts.
A Game-Changer
Remember the good old days when we used "revert with message" to throw an exception in our contracts? Sure, it did the job, but it had some quirks that sometimes led to unexpected pitfalls. Firstly, it lacked clarity and a standardized format for error messages, making it hard for users to grasp what went wrong. On top of that, the extra gas consumption for those string messages meant higher transaction costs - ouch! Not to mention, it didn't allow us to differentiate between various types of errors easily, hampering our error handling strategies. Did you want to include the value of a variable in the error message? Sorry, not supported (you needed external libraries).
Welcome Custom Errors!
Now, let's dive into the cool stuff - Custom Errors! Solidity version 0.8.4 gave us the "error" keyword, and it's a game-changer. Similarly to the event keyword that allows us to create and emit customized events that can be uniquely identified. Using this new keyword we can now explicitly define our error types, including their own arguments, and then "emit" an instance of the correct error when we need to revert.
Let's See It in Action!
Alright, let's take a sneak peek into a simplified example of custom errors in action:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
error CustomError(string message, uint256 number);
contract CustomErrorsExample {
function foo(uint256 number) external {
if (number >= 100) {
revert CustomError("Number must be less than 100.", number);
}
// Rest of the function's logic.
}
}
Look how simple it is to include a message along with all required data: the client will can identify the exact error that was emitted by the revert call, [ABI-] decode the arguments that where passed, and correctly handle it. How awesome is that?
Benefits of Custom Errors
The benefits we get from custom errors are just amazing! First and foremost, we can create different error types for various scenarios. This gives us the flexibility to implement targeted and specific error handling strategies in the client. Say goodbye to generic catch-alls!
But that's not all! Brace yourself for better gas efficiency. Yes, you heard it right! Unlike reverting with a string, contracts that use custom errors are cheaper to deploy, and cheaper to execute, resulting in happier users.
Finally, by passing extra data with the error message, we provide users with detailed insights into what caused the error. Users no longer need to play the guessing game when something goes wrong - they'll receive all the essential information right there in the error message.
Wrap Up
Let's upgrade our error-handling game and dive into custom errors. It's time to craft decentralized applications that are more efficient and more easy to interact with!
If you want to learn more about Custom Errors in Solidity, check out the official Blog post at https://soliditylang.org/blog/2021/04/21/custom-errors/
Happy coding!
Posted on August 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.