Skip to content
On this page

encodeErrorResult

Encodes a reverted error from a function call. The opposite of decodeErrorResult.

Install

ts
import { encodeErrorResult } from 'viem'

Usage

ts
import { decodeErrorResult } from 'viem'

const value = decodeErrorResult({
  abi: wagmiAbi,
  errorName: 'InvalidTokenError',
  args: ['sold out']
})
// 0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000
ts
export const wagmiAbi = [
  ...
  {
		inputs: [
			{
				internalType: "string",
				name: "reason",
				type: "string"
			}
		],
		name: "InvalidTokenError",
		type: "error"
	},
  ...
] as const;
ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})

Return Value

Hex

The encoded error.

Parameters

abi

The contract's ABI.

ts
const value = decodeErrorResult({
  abi: wagmiAbi, 
  errorName: 'InvalidTokenError',
  args: ['sold out']
})

errorName

  • Type: string

The error name on the ABI.

ts
const value = decodeErrorResult({
  abi: wagmiAbi,
  errorName: 'InvalidTokenError', 
  args: ['sold out']
})

args (optional)

  • Type: Inferred.

Arguments (if required) to pass to the error.

ts
const value = decodeErrorResult({
  abi: wagmiAbi,
  errorName: 'InvalidTokenError',
  args: ['sold out'] 
})

Released under the MIT License.