Kit
Functions

isProgramError

isProgramError<TProgramErrorCode>(error, transactionMessage, programAddress, code?): error is Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> & SolanaError<4615026>

Identifies whether an error -- typically caused by a transaction failure -- is a custom program error from the provided program address.

Type Parameters

Type Parameter
TProgramErrorCode extends number

Parameters

ParameterTypeDescription
errorunknown-
transactionMessage{ instructions: Record<number, { programAddress: Address; }>; }The transaction message that failed to execute. Since the RPC response only provides the index of the failed instruction, the transaction message is required to determine its program address
transactionMessage.instructionsRecord<number, { programAddress: Address; }>-
programAddressAddressThe address of the program from which the error is expected to have originated
code?TProgramErrorCodeThe expected error code of the custom program error. When provided, the function will check that the custom program error code matches the given value.

Returns

error is Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> & SolanaError<4615026>

Example

try {
    // Send and confirm your transaction.
} catch (error) {
    if (isProgramError(error, transactionMessage, myProgramAddress, 42)) {
        // Handle custom program error 42 from this program.
    } else if (isProgramError(error, transactionMessage, myProgramAddress)) {
        // Handle all other custom program errors from this program.
    } else {
        throw error;
    }
}

On this page