@wiko/errors
Version:
Throw, identify, and decode Wiko JavaScript errors
75 lines • 3.4 kB
TypeScript
import { WikoErrorCode, WikoErrorCodeWithCause } from './codes';
import { WikoErrorContext } from './context';
/**
* A type guard that returns `true` if the input is a {@link WikoError}, optionally with a
* particular error code.
*
* When the `code` argument is supplied and the input is a {@link WikoError}, TypeScript will
* refine the error's {@link WikoError#context | `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.
*
* @example
* ```ts
* import {
* WIKO_ERROR__TRANSACTION__MISSING_SIGNATURE,
* WIKO_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING,
* isWikoError,
* } from '@wiko/errors';
* import { assertTransactionIsFullySigned, getSignatureFromTransaction } from '@wiko/transactions';
*
* try {
* const transactionSignature = getSignatureFromTransaction(tx);
* assertTransactionIsFullySigned(tx);
* /* ... *\/
* } catch (e) {
* if (isWikoError(e, WIKO_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 (isWikoError(e, WIKO_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;
* }
* ```
*/
export declare function isWikoError<TErrorCode extends WikoErrorCode>(e: unknown,
/**
* When supplied, this function will require that the input is a {@link WikoError} _and_ that
* its error code is exactly this value.
*/
code?: TErrorCode): e is WikoError<TErrorCode>;
type WikoErrorCodedContext = Readonly<{
[P in WikoErrorCode]: (WikoErrorContext[P] extends undefined ? object : WikoErrorContext[P]) & {
__code: P;
};
}>;
/**
* Encapsulates an error's stacktrace, a Wiko-specific numeric code that indicates what went
* wrong, and optional context if the type of error indicated by the code supports it.
*/
export declare class WikoError<TErrorCode extends WikoErrorCode = WikoErrorCode> extends Error {
/**
* Indicates the root cause of this {@link WikoError}, if any.
*
* For example, a transaction error might have an instruction error as its root cause. In this
* case, you will be able to access the instruction error on the transaction error as `cause`.
*/
readonly cause?: TErrorCode extends WikoErrorCodeWithCause ? WikoError : unknown;
/**
* Contains context that can assist in understanding or recovering from a {@link WikoError}.
*/
readonly context: WikoErrorCodedContext[TErrorCode];
constructor(...[code, contextAndErrorOptions]: WikoErrorContext[TErrorCode] extends undefined ? [code: TErrorCode, errorOptions?: ErrorOptions | undefined] : [code: TErrorCode, contextAndErrorOptions: WikoErrorContext[TErrorCode] & (ErrorOptions | undefined)]);
}
export {};
//# sourceMappingURL=error.d.ts.map