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.
ts
import { type GetBlockNumberErrorType } from 'viem'
import { publicClient } from './client'
try {
const blockNumber = await client.getBlockNumber()
} catch (e) {
const error = e as GetBlockNumberErrorType
error.name
// ^? (property) name: "Error" | "ChainDisconnectedError" | "HttpRequestError" | "InternalRpcError" | "InvalidInputRpcError" | "InvalidParamsRpcError" | "InvalidRequestRpcError" | "JsonRpcVersionUnsupportedError" | ... 16 more ... | "WebSocketRequestError"
if (error.name === 'InternalRpcError')
error.code
// ^? (property) code: -32603
if (error.name === 'HttpRequestError')
error.headers
// ^? (property) headers: Headers
error.status
// ^? (property) status: number
}
import { type GetBlockNumberErrorType } from 'viem'
import { publicClient } from './client'
try {
const blockNumber = await client.getBlockNumber()
} catch (e) {
const error = e as GetBlockNumberErrorType
error.name
// ^? (property) name: "Error" | "ChainDisconnectedError" | "HttpRequestError" | "InternalRpcError" | "InvalidInputRpcError" | "InvalidParamsRpcError" | "InvalidRequestRpcError" | "JsonRpcVersionUnsupportedError" | ... 16 more ... | "WebSocketRequestError"
if (error.name === 'InternalRpcError')
error.code
// ^? (property) code: -32603
if (error.name === 'HttpRequestError')
error.headers
// ^? (property) headers: Headers
error.status
// ^? (property) status: number
}
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})