ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
22.64k stars 5.61k forks source link

require() to accept custom errors instead of a message #15214

Closed CedarMist closed 4 days ago

CedarMist commented 4 days ago

The require(cond, message); syntax is nice and lets you think clearly about the restrictions you're enforcing because the condition is a positive (it's easier to think 'when this condition is true, do X', versus 'when this condition isn't true, do X').

require( n <= MAX_NFTS_PER_COLLECTION, "ec.TOOMANY" );

And custom errors are nice, because it lets you include additional contextual information along-side an error. However to raise custom errors you must invert the condition which can be slightly more annoying to think about.

if(n > MAX_NFTS_PER_COLLECTION) revert ExtendedTooMany(collectionId)

I suggest that the second parameter of require be adjusted to either pass a message string or an expression which returns an Error type:

require( n <= MAX_NFTS_PER_COLLECTION, ExtendedTooMany(collectionId) );
r0qs commented 4 days ago

Hi @CedarMist, this feature is already supported in Solidity v0.8.26 in via-ir codegen pipeline (you can read more about it in our release blog post). And it was recently added to legacy codegen in https://github.com/ethereum/solidity/pull/15174, but it is not released yet, it will be available for legacy codegen in the next release.

CedarMist commented 4 days ago

My bad, I tried to search before submitting.

Glad to hear this will be in the upcoming release