Skip to content
LogoLogo

Error Handling

Every module in viem exports an accompanying error type which you can use to strongly type your catch statements.

These types come in the form of <Module>ErrorType. For example, the getBlockNumber action exports a GetBlockNumberErrorType type.

Unfortunately, TypeScript doesn't have an abstraction for typed exceptions, so the most pragmatic & vanilla approach would be to explicitly cast error types in the catch statement.

import { type  } from 'viem'
import {  } from './client'
 
try {
  const  = await .()
} catch () {
  const  =  as 
  . 
 
 
 
 
 
 
  if (. === 'InternalRpcError')
    .
 
 
  if (. === 'HttpRequestError') {
    .
 
 
    .
  }
}