@metamask/snaps-sdk
Version:
A library containing the core functionality for building MetaMask Snaps
125 lines • 4.14 kB
text/typescript
import type { Json, JsonRpcError } from "@metamask/utils";
import { SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "./internals/index.mjs";
import type { TrackableError } from "./types/index.mjs";
/**
* A generic error which can be thrown by a Snap, without it causing the Snap to
* crash.
*/
export declare class SnapError extends Error {
#private;
/**
* Create a new `SnapError`.
*
* @param error - The error to create the `SnapError` from. If this is a
* `string`, it will be used as the error message. If this is an `Error`, its
* `message` property will be used as the error message. If this is a
* `JsonRpcError`, its `message` property will be used as the error message
* and its `code` property will be used as the error code. Otherwise, the
* error will be converted to a string and used as the error message.
* @param data - Additional data to include in the error. This will be merged
* with the error data, if any.
*/
constructor(error: string | Error | JsonRpcError, data?: Record<string, Json>);
/**
* The error name.
*
* @returns The error name.
*/
get name(): string;
/**
* The error code.
*
* @returns The error code.
*/
get code(): number;
/**
* The error message.
*
* @returns The error message.
*/
get message(): string;
/**
* Additional data for the error.
*
* @returns Additional data for the error.
*/
get data(): Record<string, Json> | undefined;
/**
* The error stack.
*
* @returns The error stack.
*/
get stack(): string | undefined;
/**
* Convert the error to a JSON object.
*
* @returns The JSON object.
*/
toJSON(): SerializedSnapError;
/**
* Serialize the error to a JSON object. This is called by
* `@metamask/rpc-errors` when serializing the error.
*
* @returns The JSON object.
*/
serialize(): SerializedSnapError;
}
/**
* A serialized {@link SnapError}. It's JSON-serializable, so it can be sent
* over the RPC. The original error is wrapped in the `cause` property.
*
* @property code - The error code. This is always `-31002`.
* @property message - The error message. This is always `'Snap Error'`.
* @property data - The error data.
* @property data.cause - The cause of the error.
* @property data.cause.code - The error code.
* @property data.cause.message - The error message.
* @property data.cause.stack - The error stack.
* @property data.cause.data - Additional data for the error.
* @see SnapError
*/
export type SerializedSnapError = {
code: typeof SNAP_ERROR_CODE;
message: typeof SNAP_ERROR_MESSAGE;
data: {
cause: JsonRpcError;
};
};
/**
* Get a serialised JSON error from a given error. This is intended to be used
* with `snap_trackError` to convert an error to a JSON object that can be
* tracked by the Sentry instance in the client.
*
* @param error - The error to convert to a JSON error. This can be a string, an
* `Error`, a `JsonRpcError`, or any other type. If it is not a string or an
* `Error`, it will be converted to a string using its `toString()` method.
* @returns A JSON object containing the error message and stack trace, if
* available.
* @example
* try {
* // Some code that may throw an error
* } catch (error) {
* await snap.request({
* method: 'snap_trackError',
* params: {
* error: getJsonError(error),
* },
* });
* }
*/
export declare function getJsonError(error: string | Error | JsonRpcError | unknown): TrackableError;
/**
* Check if a JSON-RPC error is a `SnapError`.
*
* @param error - The object to check.
* @returns Whether the object is a `SnapError`.
*/
export declare function isSerializedSnapError(error: JsonRpcError): error is SerializedSnapError;
/**
* Check if an object is a `SnapError`.
*
* @param error - The object to check.
* @returns Whether the object is a `SnapError`.
*/
export declare function isSnapError(error: unknown): error is SnapError;
//# sourceMappingURL=errors.d.mts.map