Skill 1.3.3

Handle reverts and contract errors returned to web3.js

Key Concepts

When a contract reverts during call(), estimateGas() or send(), web3.js v4 throws a ContractExecutionError. If the contract used require with a message, the error carries that reason string; if it used a custom error such as revert ZeroAmount(), web3.js decodes the error name and arguments from the ABI. Wrapping calls in try/catch lets the application display the reason.

Because send() normally estimates gas first, most reverts are caught before a transaction is broadcast, so the user pays nothing. A transaction that is sent with a fixed gas limit and then reverts on chain is mined with status 0, and its gas is consumed. Other errors to handle include a user rejecting the request in the wallet, insufficient funds for gas and a nonce that is too low.