Web3 Error Handling

Comprehensive guide for web3 error handling covering essential concepts, practical examples, and production best practices.

Web3 systems span multiple layers: clients, nodes, smart contracts, decentralized storage, and cross-chain messaging. Errors in this stack are often asynchronous, distributed, and opaque. Web3 Error Handling is the practice of capturing, diagnosing, and responding to failures across these layers, from transaction submission to finality, with consistent structure and actionable context. It matters when a user sends a transaction that fails silently, when a node misses a block, when a contract reverts with unclear data, or when a dApp fails to load due to a missing event from a remote chain.

Diagnosing Transaction Failures

A transaction is the primary unit of work in Web3. When it fails, the root cause may lie in the client, the network, the contract, or the node. The most common failure is a transaction receipt with status: 0 and no logs, indicating a revert.

To diagnose, start with the raw transaction hash. Use eth_getTransactionByHash to retrieve the full transaction object:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "hash": "0x...",
    "nonce": "0x1",
    "blockHash": "0x...",
    "blockNumber": "0x1",
    "transactionIndex": "0x1",
    "from": "0x...",
    "to": "0x...",
    "value": "0x1",
    "gasPrice": "0x1",
    "gas": "0x1",
    "input": "0x...",
    "v": "0x1",
    "r": "0x...",
    "s": "0x...",
    "status": "0x0"
  }
}

A status: 0 means the transaction failed. Next, fetch the transaction receipt with eth_getTransactionReceipt:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "transactionHash": "0x...",
    "transactionIndex": "0x1",
    "blockHash": "0x...",
    "blockNumber": "0x1",
    "from": "0x...",
    "to": "0x...",
    "cumulativeGasUsed": "0x1",
    "gasUsed": "0x1",
    "contractAddress": null,
    "logs": [],
    "status": "0x0"
  }
}

The logs array is critical. If it’s empty, the contract reverted but emitted no logs. Use eth_getLogs to query for events from the same block range:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_getLogs",
  "params": [
    {
      "fromBlock": "0x1",
      "toBlock": "0x1",
      "address": [ "0x..." ],
      "topics": [ null, [ "0x..." ] ]
    }
  ]
}

If no logs appear, the transaction likely reverted in a require or revert statement. Decode the input field using the ABI. For a contract function deposit(uint256 amount), the input starts with 0x4b2c4d0f..., where 0x4b2c4d0f is the function selector.

To extract the revert reason, use eth_call with the same transaction input and a to address of 0x... (the contract address), and a data field of 0x20010000:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_call",
  "params": [
    {
      "to": "0x...",
      "data": "0x20010000"
    },
    "latest"
  ]
}

The response is the reason bytes. If the reason is 0x08c379a0, it indicates a Panic(uint256) with a PanicCode of 0x08. For a require statement, the reason is 0x08c379a0 followed by the length and the string.

The failure mode most silently ignored is gasLimit vs gasUsed. A transaction may use 200,000 gas but have a gasLimit of 210,000, yet the status remains 0. This happens when the transaction reverts *after* the gas limit is exceeded but before the return data is written. The gasUsed field in the receipt is key: if it exceeds gasLimit, the transaction ran out of gas and the return data is truncated.

Handling Contract Reverts

When a contract reverts, the return data contains a reason. The most common format is abi.encodePacked(string reason) for revert("message"), and abi.encodePacked(uint256 code) for revert(uint256).

For a revert with abi.encodePacked(bytes4 selector, bytes reason) (e.g., Error(string)), decode the first 4 bytes as the Error(string) selector. The remainder is the string.

Use eth_getCode to retrieve the contract’s bytecode, then extract the ABI from the metadata field of the contract object in the build output. Use the ABI to decode events and function calls.

The sharp edge: abi.encodePacked vs abi.encode. In abi.encodePacked, strings are packed without padding, so abi.encodePacked("hello", "world") is 0x0568656c6c6f05776f726c64. But abi.encode adds a length prefix: 0x0000000a68656c6c6f0000000a776f726c64.

When reading logs, use topics[0] to identify the event signature. For Deposit(address indexed from, uint256 amount), the first topic is 0x82d87a3b..., and topics[1] is the from address.

If the event is emitted but not indexed, the from address appears in data, not in topics. To find it, decode the data field with the ABI.

Monitoring Node and Network Health

A node may fail to serve requests, miss blocks, or fall behind. The eth_syncing RPC returns a syncing object:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "startingBlock": "0x1",
    "currentBlock": "0x100",
    "highestBlock": "0x105",
    "knownBlocks": "0x105",
    "latestBlock": "0x105",
    "syncedBlocks": "0x5"
  }
}

A node is syncing when startingBlock < currentBlock. If currentBlock < highestBlock, the node is behind. Use eth_blockNumber to monitor the latest block. A sudden lag indicates network congestion or node failure.

Use eth_getBlockByNumber with fullTransactions: true to fetch a block with all its transactions. If the block’s transactions array is empty, the node may have failed to fetch transactions from the network.

The eth_getFilterChanges RPC is critical for event processing. Create a filter with eth_newFilter:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_newFilter",
  "params": [
    {
      "fromBlock": "0x1",
      "toBlock": "latest",
      "address": "0x...",
      "topics": [ "0x..." ]
    }
  ]
}

The response is a filterId (e.g., 0x1). Poll eth_getFilterChanges with that ID. A null response indicates no new events. If eth_getFilterChanges returns an empty array, the filter is stuck.

The most common silent failure: eth_getFilterChanges returns an array of hashes but the logs field is missing. The node is not configured to include logs in the filter. This happens when the filter is created with eth_newFilter but the node does not emit logs in eth_getFilterChanges.

Handling Cross-Chain Message Failures

When a message crosses chains, it may fail at any step: message submission, confirmation, delivery, or execution.

Use eth_getTransactionReceipt on the message submission transaction. If status: 0, the message was submitted but failed to be confirmed.

To verify confirmation, query the destination chain for the message. Use eth_call with the verifyMessage function of the cross-chain messenger contract:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_call",
  "params": [
    {
      "to": "0x...",
      "data": "0x345123a1...",
      "from": "0x..."
    },
    "latest"
  ]
}

The data field includes the messageId, sourceChainId, and destinationChainId.

If the message is confirmed, use eth_getLogs to find the MessageReceived event. The event includes messageId, sender, recipient, data, and nonce.

A message may be delivered but not executed. The MessageExecuted event may be missing. To detect this, use eth_call on executedMessages(messageId).

The sharp edge: messageId is not unique across chains. A message can be submitted on chain A with messageId = 100, and another message on chain B with the same messageId. The messageId is only unique per chain.

Use eth_getBlockByNumber on the destination chain to check if the MessageReceived event was mined. If the event is in a block but the MessageExecuted event is not, the message was delivered but not executed.

To recover, use eth_call to execute the message manually. The execute function takes the messageId and data. If the execution fails, the return data contains the revert reason.

Responding to Errors in dApps

dApps must handle errors from multiple sources: client, network, contract, and node.

When a user submits a transaction, the dApp should:

  1. Show a transaction hash.
  2. Poll eth_getTransactionReceipt until the transaction is confirmed.
  3. If status: 0, fetch eth_getLogs for the transaction’s block.
  4. If logs are missing, decode the input field and call eth_call with the same input.
  5. If the return data is non-empty, decode it as a revert reason.
  6. If the return data is a bytes array, use abi.decode to extract the error.

For example, if the return data is 0x08c379a00000000000000000000000000000000000000000000000000000000000000000, decode it as Panic(uint256) with PanicCode = 0x08.

The dApp should also handle eth_syncing and eth_blockNumber to show a sync status. When the node is syncing, the dApp can show a loading spinner and disable certain actions.

To handle network failures, use eth_getBlockByNumber with fullTransactions: true to fetch a block. If the transactions array is missing, the network is down or the node is unreachable.

Finally, use eth_getCode to verify a contract is deployed. If eth_getCode returns 0x, the contract is not deployed.

Handling Timeouts and Retries

Web3 systems are asynchronous. A transaction may take minutes to finalize. The dApp must handle timeouts.

Use eth_getTransactionReceipt with a timeout of 30 seconds. If the receipt is not received, retry with eth_getTransactionByHash and eth_call to check the transaction’s status.

If the transaction is pending, the dApp can poll eth_getTransactionReceipt every 5 seconds. After 10 minutes, if the transaction is still pending, assume it will fail.

For long-running operations, use eth_getFilterChanges with a filter ID. If eth_getFilterChanges returns null, the filter is stale. The dApp should refresh the filter or create a new one.

Use eth_call with latest block to check the current time. If the block time is more than 15 seconds behind, the node is slow or the network is congested.

The sharp edge: eth_getLogs returns null when no logs are available. The dApp should not assume logs is an array. It may be null, [], or null in the result field.

Summary of Key Patterns

This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.