UNPKG

@saberhq/sail

Version:

Account caching and batched loading for React-based Solana applications.

251 lines 8.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SailSignAndConfirmError = exports.SailGetMultipleAccountsError = exports.SailAccountLoadError = exports.SailProgramAccountParseError = exports.SailAccountParseError = exports.SailCacheRefetchError = exports.SailTransactionSignError = exports.SailRefetchSubscriptionsError = exports.SailRefetchAfterTXError = exports.InsufficientSOLError = exports.SailTransactionError = exports.SailUnknownTXFailError = exports.SailError = exports.extractErrorMessage = exports.ERROR_TITLES = void 0; const categorizeTransactionError_1 = require("./categorizeTransactionError"); exports.ERROR_TITLES = { SailUnknownTXFailError: "Transaction failed", SailTransactionError: "Transaction processing failed", SailInsufficientSOLError: "Insufficient SOL balance", SailRefetchAfterTXError: "Error fetching changed accounts", SailRefetchSubscriptionsError: "Error refetching subscribed accounts", SailTransactionSignError: "Error signing transactions", SailCacheRefetchError: "Error refetching from cache", SailAccountParseError: "Error parsing account", SailProgramAccountParseError: "Error parsing program account", SailAccountLoadError: "Error loading account", SailGetMultipleAccountsError: "Error fetching multiple accounts", SailSignAndConfirmError: "Error signing and confirming transactions", }; /** * Extracts the message from an error. * @param errLike Error-like object. * @returns */ const extractErrorMessage = (errLike) => { var _a; return "message" in errLike ? (_a = errLike.message) !== null && _a !== void 0 ? _a : null : null; }; exports.extractErrorMessage = extractErrorMessage; /** * Error originating from Sail. */ class SailError extends Error { constructor( /** * Name of the Sail error. */ sailErrorName, /** * The original error thrown, if applicable. */ originalError, /** * Underlying error message. */ cause) { var _a; if (cause === void 0) { cause = `${(_a = (0, exports.extractErrorMessage)(originalError)) !== null && _a !== void 0 ? _a : "unknown"}`; } super(`${exports.ERROR_TITLES[sailErrorName]}: ${cause}`); this.sailErrorName = sailErrorName; this.originalError = originalError; this.cause = cause; this._isSailError = true; this.name = sailErrorName; if (originalError instanceof Error) { this.stack = originalError.stack; } } /** * Title of the error. */ get title() { return exports.ERROR_TITLES[this.sailErrorName]; } } exports.SailError = SailError; /** * Returns the value as a {@link SailError}, or null if it isn't. * @param e * @returns */ SailError.tryInto = (e) => { return e instanceof SailError || ("_isSailError" in e && e._isSailError) ? e : null; }; /** * Error originating from Sail. */ class SailUnknownTXFailError extends SailError { constructor(originalError, network, txs) { super("SailUnknownTXFailError", originalError); this.network = network; this.txs = txs; } } exports.SailUnknownTXFailError = SailUnknownTXFailError; /** * Error on a Solana transaction */ class SailTransactionError extends SailError { constructor(network, originalError, tx, /** * User message representing the transaction. */ userMessage) { super("SailTransactionError", originalError); this.network = network; this.tx = tx; this.userMessage = userMessage; } /** * Tag used for grouping errors together. */ get tag() { return (0, categorizeTransactionError_1.categorizeTransactionError)(this.message); } /** * Returns true if this error is associated with a simulation. */ get isSimulation() { return this.message.includes("Transaction simulation failed: "); } /** * Fingerprint used for grouping errors. */ get fingerprint() { const tag = this.tag; if (tag) { return [this.name, tag]; } return [this.name, ...this.message.split(": ")]; } /** * Generates a debug string representation of the transactions involved in this error. * @param network * @returns */ generateLogMessage() { const parts = [this.tx.debugStr]; if (this.network !== "localnet") { parts.push(`Inspect transaction details: ${this.tx.generateInspectLink(this.network)}`); } return parts.join("\n"); } } exports.SailTransactionError = SailTransactionError; /** * Thrown if there is not enough SOL to pay for a transaction. */ class InsufficientSOLError extends SailError { constructor(currentBalance) { super("SailInsufficientSOLError", null, "Insufficient SOL balance"); this.currentBalance = currentBalance; } } exports.InsufficientSOLError = InsufficientSOLError; /** * Thrown if there is an error refetching accounts after a transaction. */ class SailRefetchAfterTXError extends SailError { constructor(originalError, writable, txs) { super("SailRefetchAfterTXError", originalError); this.writable = writable; this.txs = txs; } /** * List of {@link TransactionSignature}s. */ get txSigs() { return this.txs.map((tx) => tx.signature); } } exports.SailRefetchAfterTXError = SailRefetchAfterTXError; /** * Thrown if an error occurs when refetching subscriptions. */ class SailRefetchSubscriptionsError extends SailError { constructor(originalError) { super("SailRefetchSubscriptionsError", originalError); } } exports.SailRefetchSubscriptionsError = SailRefetchSubscriptionsError; /** * Thrown if transactions could not be signed. */ class SailTransactionSignError extends SailError { constructor(originalError, txs) { super("SailTransactionSignError", originalError); this.txs = txs; } } exports.SailTransactionSignError = SailTransactionSignError; /** * Thrown if a cache refetch results in an error. */ class SailCacheRefetchError extends SailError { constructor(originalError, keys) { super("SailCacheRefetchError", originalError); this.keys = keys; } } exports.SailCacheRefetchError = SailCacheRefetchError; /** * Thrown if there is an error parsing an account. */ class SailAccountParseError extends SailError { constructor(originalError, data) { super("SailAccountParseError", originalError); this.data = data; } } exports.SailAccountParseError = SailAccountParseError; /** * Thrown if there is an error parsing an account. */ class SailProgramAccountParseError extends SailError { constructor(originalError, data, parser) { super("SailProgramAccountParseError", originalError); this.data = data; this.parser = parser; } } exports.SailProgramAccountParseError = SailProgramAccountParseError; /** * Thrown if an account could not be loaded. */ class SailAccountLoadError extends SailError { constructor(originalError, accountId) { super("SailAccountLoadError", originalError); this.accountId = accountId; } get userMessage() { return `Error loading account ${this.accountId.toString()}`; } } exports.SailAccountLoadError = SailAccountLoadError; /** * Callback called whenever getMultipleAccounts fails. */ class SailGetMultipleAccountsError extends SailError { constructor(keys, commitment, originalError) { super("SailGetMultipleAccountsError", originalError); this.keys = keys; this.commitment = commitment; } } exports.SailGetMultipleAccountsError = SailGetMultipleAccountsError; /** * Callback called whenever getMultipleAccounts fails. */ class SailSignAndConfirmError extends SailError { constructor(errors) { super("SailSignAndConfirmError", errors); this.errors = errors; } } exports.SailSignAndConfirmError = SailSignAndConfirmError; //# sourceMappingURL=errors.js.map