UNPKG

@metamask/snaps-sdk

Version:

A library containing the core functionality for building MetaMask Snaps

120 lines 4.72 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _SnapError_code, _SnapError_message, _SnapError_data, _SnapError_stack; import { getErrorCode, getErrorData, getErrorMessage, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "./internals/index.mjs"; /** * A generic error which can be thrown by a Snap, without it causing the Snap to * crash. */ export class SnapError extends Error { /** * 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, data = {}) { const message = getErrorMessage(error); super(message); _SnapError_code.set(this, void 0); _SnapError_message.set(this, void 0); _SnapError_data.set(this, void 0); _SnapError_stack.set(this, void 0); __classPrivateFieldSet(this, _SnapError_message, message, "f"); __classPrivateFieldSet(this, _SnapError_code, getErrorCode(error), "f"); const mergedData = { ...getErrorData(error), ...data }; if (Object.keys(mergedData).length > 0) { __classPrivateFieldSet(this, _SnapError_data, mergedData, "f"); } __classPrivateFieldSet(this, _SnapError_stack, super.stack, "f"); } /** * The error name. * * @returns The error name. */ get name() { return 'SnapError'; } /** * The error code. * * @returns The error code. */ get code() { return __classPrivateFieldGet(this, _SnapError_code, "f"); } /** * The error message. * * @returns The error message. */ // This line is covered, but Jest doesn't pick it up for some reason. /* istanbul ignore next */ get message() { return __classPrivateFieldGet(this, _SnapError_message, "f"); } /** * Additional data for the error. * * @returns Additional data for the error. */ get data() { return __classPrivateFieldGet(this, _SnapError_data, "f"); } /** * The error stack. * * @returns The error stack. */ // This line is covered, but Jest doesn't pick it up for some reason. /* istanbul ignore next */ get stack() { return __classPrivateFieldGet(this, _SnapError_stack, "f"); } /** * Convert the error to a JSON object. * * @returns The JSON object. */ toJSON() { return { code: SNAP_ERROR_CODE, message: SNAP_ERROR_MESSAGE, data: { cause: { code: this.code, message: this.message, stack: this.stack, ...(this.data ? { data: this.data } : {}), }, }, }; } /** * Serialize the error to a JSON object. This is called by * `@metamask/rpc-errors` when serializing the error. * * @returns The JSON object. */ serialize() { return this.toJSON(); } } _SnapError_code = new WeakMap(), _SnapError_message = new WeakMap(), _SnapError_data = new WeakMap(), _SnapError_stack = new WeakMap(); //# sourceMappingURL=errors.mjs.map