@saberhq/sail
Version:
Account caching and batched loading for React-based Solana applications.
234 lines • 6.84 kB
JavaScript
import { categorizeTransactionError } from "./categorizeTransactionError";
export const 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
*/
export const extractErrorMessage = (errLike) => {
var _a;
return "message" in errLike
? (_a = errLike.message) !== null && _a !== void 0 ? _a : null
: null;
};
/**
* Error originating from Sail.
*/
export 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 = extractErrorMessage(originalError)) !== null && _a !== void 0 ? _a : "unknown"}`; }
super(`${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 ERROR_TITLES[this.sailErrorName];
}
}
/**
* 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.
*/
export class SailUnknownTXFailError extends SailError {
constructor(originalError, network, txs) {
super("SailUnknownTXFailError", originalError);
this.network = network;
this.txs = txs;
}
}
/**
* Error on a Solana transaction
*/
export 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 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");
}
}
/**
* Thrown if there is not enough SOL to pay for a transaction.
*/
export class InsufficientSOLError extends SailError {
constructor(currentBalance) {
super("SailInsufficientSOLError", null, "Insufficient SOL balance");
this.currentBalance = currentBalance;
}
}
/**
* Thrown if there is an error refetching accounts after a transaction.
*/
export 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);
}
}
/**
* Thrown if an error occurs when refetching subscriptions.
*/
export class SailRefetchSubscriptionsError extends SailError {
constructor(originalError) {
super("SailRefetchSubscriptionsError", originalError);
}
}
/**
* Thrown if transactions could not be signed.
*/
export class SailTransactionSignError extends SailError {
constructor(originalError, txs) {
super("SailTransactionSignError", originalError);
this.txs = txs;
}
}
/**
* Thrown if a cache refetch results in an error.
*/
export class SailCacheRefetchError extends SailError {
constructor(originalError, keys) {
super("SailCacheRefetchError", originalError);
this.keys = keys;
}
}
/**
* Thrown if there is an error parsing an account.
*/
export class SailAccountParseError extends SailError {
constructor(originalError, data) {
super("SailAccountParseError", originalError);
this.data = data;
}
}
/**
* Thrown if there is an error parsing an account.
*/
export class SailProgramAccountParseError extends SailError {
constructor(originalError, data, parser) {
super("SailProgramAccountParseError", originalError);
this.data = data;
this.parser = parser;
}
}
/**
* Thrown if an account could not be loaded.
*/
export class SailAccountLoadError extends SailError {
constructor(originalError, accountId) {
super("SailAccountLoadError", originalError);
this.accountId = accountId;
}
get userMessage() {
return `Error loading account ${this.accountId.toString()}`;
}
}
/**
* Callback called whenever getMultipleAccounts fails.
*/
export class SailGetMultipleAccountsError extends SailError {
constructor(keys, commitment, originalError) {
super("SailGetMultipleAccountsError", originalError);
this.keys = keys;
this.commitment = commitment;
}
}
/**
* Callback called whenever getMultipleAccounts fails.
*/
export class SailSignAndConfirmError extends SailError {
constructor(errors) {
super("SailSignAndConfirmError", errors);
this.errors = errors;
}
}
//# sourceMappingURL=errors.js.map