Function isSolanaError

  • A type guard that returns true if the input is a SolanaError, optionally with a particular error code.

    When the code argument is supplied and the input is a SolanaError, TypeScript will refine the error's context property to the type associated with that error code. You can use that context to render useful error messages, or to make context-aware decisions that help your application to recover from the error.

    Type Parameters

    Parameters

    • e: unknown
    • Optionalcode: TErrorCode

      When supplied, this function will require that the input is a SolanaError and that its error code is exactly this value.

    Returns e is SolanaError<TErrorCode>

    import {
    SOLANA_ERROR__TRANSACTION__MISSING_SIGNATURE,
    SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING,
    isSolanaError,
    } from '@solana/errors';
    import { assertTransactionIsFullySigned, getSignatureFromTransaction } from '@solana/transactions';

    try {
    const transactionSignature = getSignatureFromTransaction(tx);
    assertTransactionIsFullySigned(tx);
    /* ... */
    } catch (e) {
    if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING)) {
    displayError(
    "We can't send this transaction without signatures for these addresses:\n- %s",
    // The type of the `context` object is now refined to contain `addresses`.
    e.context.addresses.join('\n- '),
    );
    return;
    } else if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING)) {
    if (!tx.feePayer) {
    displayError('Choose a fee payer for this transaction before sending it');
    } else {
    displayError('The fee payer still needs to sign for this transaction');
    }
    return;
    }
    throw e;
    }