[Actionable List] Replace revert strings with custom errors
See original GitHub issue🧐 Motivation
Custom Errors, which were introduced in 0.8.4, contribute to reduce the contract size and improve clarity of revert reasons.
A quick study shows that replacing a (short) revert string with a custom error can save 8.5k gas, and slightly decrease the run cost.
error SomeError();
error SomeErrorWithArgs(address);
contract A { function test() external { revert("some short message"); } }
// deploy: 90599
// run: 21288
contract B { function test() external { revert("some long message that will cause deployment to be more expensive, but hopefully not execution"); } }
// deploy: 109401
// run: 21318
contract C { function test() external { revert SomeError(); } }
// deploy: 81953
// run: 21228
contract D { function test() external { revert SomeErrorWithArgs(msg.sender); } }
// deploy: 83225
// run: 21245
📝 Next steps
There are some question to resolve before moving forward with that change:
- Check/validate custom error support by developer tools (hardhat + truffle / hardhat + waffle, etherscan, …)
- Adapt our tests if needed
- List all revert string, and decide what custom error to use for each one
- some custom errors might be reusable between contracts (ex:
InsufficientBalance
for ERCs 20, 777, 1155) - decide which parameter (if any) to include in the custom error
- some custom errors might be reusable between contracts (ex:
- Decide where to define the errors
- In each file: makes re-use difficult
- In a global file: messy
- Other?
See
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Custom errors, extending Error - The Modern JavaScript Tutorial
Now custom errors are much shorter, especially ValidationError , as we got rid of the "this.name = ..." line in the constructor.
Read more >Solidity revert with custom error explained with example
The new method allows users to define a custom error and pass it while reverting a transaction. Custom error. Custom errors are defined...
Read more >Error handling - Apollo GraphQL Docs
Making errors actionable on the client and server. ... You can create a custom errors and codes using the graphql package's GraphQLError class,...
Read more >ASP.NET MVC Custom Errors - Stack Overflow
The key seems to be the existingResponse="Replace" attribute that I was missing. Great suggestions, the only thing I'd push back on is the ......
Read more >Displaying a Custom Error Page (C#) - Microsoft Learn
The answer depends on how the website's <customErrors> configuration. ... a connection to the database specified by a connection string.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There are two testing development tools had supported the custom error.
I recommend hardhat-chai-matchers. it provide better readability.
but waffle provider a matcher combined reason string and custom error
I believe both supports Custom Errors, and the implementation can be tested with either.
We use Custom Error all over our codebase where possible. It is lighter.