2

In the spirit of Are there well-solved and simple storage patterns for Solidity?...

Are there known, battle-tested practices for how to deal with errors in large Solidity codebases?

It's common in software engineering to have a clear collection of error names, codes and descriptions, usually implemented via enums. The only project I've seen which adheres to this rigorous take on error handling is Compound, but there must be more. Compound's codebase is big and interwoven with other finesses like formal verification scripts, so their's is not strictly the best implementation to refer to when answering this question.

4
  • I feel this question is too abstract to be answered in a meaningful manner. What is an error? Is it a user error? Configuration error? Security problem? Return value? Exception? Commented May 23, 2020 at 13:50
  • I politely disagree. It is a broad question, but so are many on this website. A solid pattern for handling errors in Solidity should not care about whether the error is a user, config, security error etc. I'm looking for tips on things like whether a library makes sense or not or what is the best syntax for implementing the enums. Commented May 23, 2020 at 19:11
  • It might be difficult to find a person who is willing to write an answer that covers this. You are likely to get more answer if you break up your question to multiple questions that are individually more answerable. Commented May 25, 2020 at 10:55
  • 2
    Well, I may do it myself. It's not against the rules of the community to open a question with the intent to answer it later if nobody else does it, is it? :) Commented May 25, 2020 at 14:28

1 Answer 1

1

Solidity v0.8.4 to the rescue!

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;

error Unauthorized();

contract VendingMachine {
    address payable owner = payable(msg.sender);

    function withdraw() public {
        if (msg.sender != owner) {
            revert Unauthorized();
        }

        owner.transfer(address(this).balance);
    }
    // ...
}

Notice the new and shiny error statement on line 4. This is a game changer for Solidity - easier to work with, more gas efficient, and more elegant than revert reason strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.