Solidity Error Handling Cheat Sheet - Require Revert Custom Errors

Solidity Error Handling Cheat Sheet - Require Revert Custom Errors

DevTools Store

Solidity Error Handling

Methods

require(condition, "msg")  // Input validation
revert("msg")              // Explicit revert
assert(condition)          // Internal invariant
custom error               // Gas efficient

Custom Errors (Best Practice)

error InsufficientBalance(uint256 have, uint256 need);
function withdraw(uint256 amt) external {
    if (amt > balance[msg.sender])
        revert InsufficientBalance(balance[msg.sender], amt);
}

Try/Catch

try external.doSomething() returns (uint r) {
    // success
} catch Error(string memory reason) {
    // require/revert
} catch Panic(uint code) {
    // assert/overflow
}

All cheat sheets: https://rentry.co/85pm7d8y

Report Page