Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Errors support in require #14442

Closed
SteMak opened this issue Jul 23, 2023 · 5 comments · Fixed by #14913 or #15174
Closed

Custom Errors support in require #14442

SteMak opened this issue Jul 23, 2023 · 5 comments · Fixed by #14913 or #15174
Assignees
Labels
feature medium effort Default level of effort must have Something we consider an essential part of Solidity 1.0.
Milestone

Comments

@SteMak
Copy link

SteMak commented Jul 23, 2023

Abstract

Since Gas-efficient Custom Errors were added in solidity 0.8.4, the require statement has become mostly unused. The if (!condition) revert CustomError() pattern sometimes looks branchy and makes requirement understanding somewhat harder.

require(min <= value && value <= max, "Value not within bounds");
// comparing to
if (min > value || value > max) revert NotWithinBounds(min, max, value);

I found a workaround, however, it consumes some more Gas compared to ordinary Custom Errors usage.

Motivation

I propose implementing Custom Errors support for the require statement, providing Custom Error as the second parameter instead of an error message.

error NotWithinBounds(uint256 min, uint256 max, uint256 actual);

function checkBounds(uint256 min, uint256 max, uint256 value) external pure {
  require(min <= value && value <= max, NotWithinBounds(min, max, value));
}

Specification

There are several opportunities:

  • Make error an ordinary type lazy loads on revert burning
    // Bytes error message is not calculated there
    require(condition, CustomError(params));
    
    function require(bool condition, error err) pure {
      if (!condition) revert err; // Bytes error message is generated here
    }
  • Implement inline functions Inline functions / closures / lambdas #12297
    require(condition, () => revert CustomError(params));
    
    function require(bool condition, function() pure handler) pure {
      if (!condition) handler();
    }
  • Make the transformation below in compile time
    require(condition, CustomError(params));
    // changed to
    if (!condition) revert CustomError(params);

Backwards Compatibility

As Solidity allows function overloading, the creation of require(bool, error, or other suitable type) should not break backward compatibility.

@SteMak SteMak added the feature label Jul 23, 2023
@r0qs r0qs added must have Something we consider an essential part of Solidity 1.0. medium effort Default level of effort labels Jul 25, 2023
@agnxsh
Copy link

agnxsh commented Jul 26, 2023

can i take this issue please?

@fulldecent
Copy link
Contributor

I fully support this issue because it increases clarity and expressiveness of the language.

It matches "guards" that other languages have. And a great programming pattern is to put all your guards at the top of a function.

Recognizing that this issue is now labeled as "must have" I have added at to the documentation at #14539

@n00b21337
Copy link

Please lets have it.
"If pattern" is really suboptimal.

@cameel cameel added this to the 0.8.25 milestone Mar 4, 2024
@cameel cameel modified the milestones: 0.8.25, 0.8.26 Mar 13, 2024
@luislucena16
Copy link

luislucena16 commented Mar 30, 2024

this could be a great implementation for 0.8.26, taking advantage of the arrival of the 0.8.25 fork!!
I will be following this issue!

@wadealexc
Copy link
Contributor

Bumping this -- we really need this feature!

The readability using custom error types is really bad - requiring branching statements for every check really clutters up the code. So we're still using require with error strings - but error strings are inflating our deployment bytecode!

It would be so awesome to be able to use require with custom errors!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature medium effort Default level of effort must have Something we consider an essential part of Solidity 1.0.
Projects
Archived in project
9 participants