encodeErrorResult
Encodes a reverted error from a function call. The opposite of decodeErrorResult
.
Install
import { encodeErrorResult } from 'viem'
Usage
import { decodeErrorResult } from 'viem'
const value = encodeErrorResult({
abi: wagmiAbi,
errorName: 'InvalidTokenError',
args: ['sold out']
})
// 0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000
Without errorName
If your abi
contains only one ABI item, you can omit the errorName
(it becomes optional):
import { decodeErrorResult } from 'viem'
const abiItem = {
inputs: [{ name: 'reason', type: 'string' }],
name: 'InvalidTokenError',
type: 'error'
}
const value = encodeErrorResult({
abi: [abiItem],
errorName: 'InvalidTokenError',
args: ['sold out']
})
// 0xb758934b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000
Return Value
The encoded error.
Parameters
abi
- Type:
Abi
The contract's ABI.
const value = decodeErrorResult({
abi: wagmiAbi,
errorName: 'InvalidTokenError',
args: ['sold out']
})
errorName
- Type:
string
The error name on the ABI.
const value = encodeErrorResult({
abi: wagmiAbi,
errorName: 'InvalidTokenError',
args: ['sold out']
})
args (optional)
- Type: Inferred.
Arguments (if required) to pass to the error.
const value = encodeErrorResult({
abi: wagmiAbi,
errorName: 'InvalidTokenError',
args: ['sold out']
})