question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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
  • Decide where to define the errors
    • In each file: makes re-use difficult
    • In a global file: messy
    • Other?

See

  • #3183 (uses custom errors)
  • #2725 (rewrite of access control around custom errors)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wiasliawcommented, Oct 13, 2022

There are two testing development tools had supported the custom error.

I recommend hardhat-chai-matchers. it provide better readability.

// hardhat-chai-matchers
await expect(contract.call()).to.be.revertedWithCustomError(
  contract,
  "SomeCustomError"
);

but waffle provider a matcher combined reason string and custom error

// waffle
// reason string
await expect(token.checkRole('ADMIN'))
  .to.be.revertedWith(/AccessControl: account .* is missing role .*/);

// custom error
await expect(token.transfer(receiver, 100))
    .to.be.revertedWith('InsufficientBalance')
    .withArgs(0, 100);
0reactions
ItsShadowlcommented, Oct 28, 2022

hardhat-chai-matchers should be preferred over Waffle nowadays.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found