@metamask/snaps-utils 11.2.0 → 11.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [11.3.0]
11
+
12
+ ### Changed
13
+
14
+ - Move `TrackableErrorStruct` from `@metamask/snaps-rpc-methods` for reusability ([#3546](https://github.com/MetaMask/snaps/pull/3546))
15
+
10
16
  ## [11.2.0]
11
17
 
12
18
  ### Added
@@ -672,7 +678,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
672
678
  - The version of the package no longer needs to match the version of all other
673
679
  MetaMask Snaps packages.
674
680
 
675
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.2.0...HEAD
681
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.3.0...HEAD
682
+ [11.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.2.0...@metamask/snaps-utils@11.3.0
676
683
  [11.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.1.0...@metamask/snaps-utils@11.2.0
677
684
  [11.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.0.0...@metamask/snaps-utils@11.1.0
678
685
  [11.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@10.1.0...@metamask/snaps-utils@11.0.0
package/dist/errors.cjs CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
3
+ exports.TrackableErrorStruct = exports.unwrapError = exports.isWrappedSnapError = exports.isSerializedSnapError = exports.isSnapError = exports.WrappedSnapError = exports.SNAP_ERROR_WRAPPER_MESSAGE = exports.SNAP_ERROR_WRAPPER_CODE = void 0;
4
4
  const rpc_errors_1 = require("@metamask/rpc-errors");
5
5
  const snaps_sdk_1 = require("@metamask/snaps-sdk");
6
+ const superstruct_1 = require("@metamask/superstruct");
6
7
  const utils_1 = require("@metamask/utils");
7
8
  exports.SNAP_ERROR_WRAPPER_CODE = -31001;
8
9
  exports.SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
@@ -184,4 +185,10 @@ function unwrapError(error) {
184
185
  ];
185
186
  }
186
187
  exports.unwrapError = unwrapError;
188
+ exports.TrackableErrorStruct = (0, superstruct_1.object)({
189
+ name: (0, superstruct_1.string)(),
190
+ message: (0, superstruct_1.string)(),
191
+ stack: (0, superstruct_1.nullable)((0, superstruct_1.string)()),
192
+ cause: (0, superstruct_1.nullable)((0, superstruct_1.lazy)(() => exports.TrackableErrorStruct)),
193
+ });
187
194
  //# sourceMappingURL=errors.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8B;AAG9B,mDAK6B;AAE7B,2CAA2D;AAE9C,QAAA,uBAAuB,GAAG,CAAC,KAAK,CAAC;AACjC,QAAA,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAa,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,IAAA,yBAAa,EAAC,K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7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAjED,kCAiEC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error) ?? undefined;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
1
+ {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,qDAI8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AAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,IAAA,yBAAa,EAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,IAAA,sBAAc,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,uBAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,IAAA,2BAAe,EAAC,KAAK,CAAC,EACtB,IAAA,yBAAa,EAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAjED,kCAiEC;AAEY,QAAA,oBAAoB,GAA2B,IAAA,oBAAM,EAAC;IACjE,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,OAAO,EAAE,IAAA,oBAAM,GAAE;IACjB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IACzB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAiB,GAAG,EAAE,CAAC,4BAAoB,CAAC,CAAC;CAClE,CAAC,CAAC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type {\n SerializedSnapError,\n SnapError,\n TrackableError,\n} from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Struct } from '@metamask/superstruct';\nimport { lazy, nullable, object, string } from '@metamask/superstruct';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error) ?? undefined;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n\nexport const TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n"]}
package/dist/errors.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { JsonRpcError as RpcError } from "@metamask/rpc-errors";
2
2
  import type { DataWithOptionalCause } from "@metamask/rpc-errors";
3
- import type { SerializedSnapError, SnapError } from "@metamask/snaps-sdk";
3
+ import type { SerializedSnapError, SnapError, TrackableError } from "@metamask/snaps-sdk";
4
+ import type { Struct } from "@metamask/superstruct";
4
5
  import type { Json, JsonRpcError } from "@metamask/utils";
5
6
  export declare const SNAP_ERROR_WRAPPER_CODE = -31001;
6
7
  export declare const SNAP_ERROR_WRAPPER_MESSAGE = "Wrapped Snap Error";
@@ -82,4 +83,5 @@ export declare function isWrappedSnapError(error: unknown): error is SerializedS
82
83
  * whether the error was handled.
83
84
  */
84
85
  export declare function unwrapError(error: unknown): [error: RpcError<DataWithOptionalCause>, isHandled: boolean];
86
+ export declare const TrackableErrorStruct: Struct<TrackableError>;
85
87
  //# sourceMappingURL=errors.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,4BAA4B;AAO1E,OAAO,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,wBAAwB;AAG1D,eAAO,MAAM,uBAAuB,SAAS,CAAC;AAC9C,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAE/D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,OAAO,uBAAuB,CAAC;IACrC,OAAO,EAAE,OAAO,0BAA0B,CAAC;IAC3C,IAAI,EAAE;QACJ,KAAK,EAAE,IAAI,CAAC;KACb,CAAC;CACH,CAAC;AAEF,qBAAa,gBAAiB,SAAQ,KAAK;;IAOzC;;;;OAIG;gBACS,KAAK,EAAE,OAAO;IAS1B;;;;OAIG;IACH,IAAI,IAAI,WAEP;IAED;;;;OAIG;IACH,IAAI,OAAO,WAEV;IAED;;;;OAIG;IACH,IAAI,KAAK,uBAER;IAED;;;;OAIG;IACH,MAAM,IAAI,0BAA0B;IAcpC;;;;;OAKG;IACH,SAAS;CAGV;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAW9D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK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AAK,IAAI,SAAS,CAW9D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,YAAY,GAClB,KAAK,IAAI,mBAAmB,CAE9B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,0BAA0B,CAMrC;AAuBD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,OAAO,GACb,CAAC,KAAK,EAAE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CA+D9D;AAED,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,cAAc,CAKtD,CAAC"}
package/dist/errors.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { JsonRpcError as RpcError } from "@metamask/rpc-errors";
2
2
  import type { DataWithOptionalCause } from "@metamask/rpc-errors";
3
- import type { SerializedSnapError, SnapError } from "@metamask/snaps-sdk";
3
+ import type { SerializedSnapError, SnapError, TrackableError } from "@metamask/snaps-sdk";
4
+ import type { Struct } from "@metamask/superstruct";
4
5
  import type { Json, JsonRpcError } from "@metamask/utils";
5
6
  export declare const SNAP_ERROR_WRAPPER_CODE = -31001;
6
7
  export declare const SNAP_ERROR_WRAPPER_MESSAGE = "Wrapped Snap Error";
@@ -82,4 +83,5 @@ export declare function isWrappedSnapError(error: unknown): error is SerializedS
82
83
  * whether the error was handled.
83
84
  */
84
85
  export declare function unwrapError(error: unknown): [error: RpcError<DataWithOptionalCause>, isHandled: boolean];
86
+ export declare const TrackableErrorStruct: Struct<TrackableError>;
85
87
  //# sourceMappingURL=errors.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.mts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,QAAQ,EAEzB,6BAA6B;AAC9B,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B;AAClE,OAAO,KAAK,EAAE,mBA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
  import { errorCodes, JsonRpcError as RpcError, serializeCause } from "@metamask/rpc-errors";
2
2
  import { getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from "@metamask/snaps-sdk";
3
+ import { lazy, nullable, object, string } from "@metamask/superstruct";
3
4
  import { isObject, isJsonRpcError } from "@metamask/utils";
4
5
  export const SNAP_ERROR_WRAPPER_CODE = -31001;
5
6
  export const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
@@ -176,4 +177,10 @@ export function unwrapError(error) {
176
177
  false,
177
178
  ];
178
179
  }
180
+ export const TrackableErrorStruct = object({
181
+ name: string(),
182
+ message: string(),
183
+ stack: nullable(string()),
184
+ cause: nullable(lazy(() => TrackableErrorStruct)),
185
+ });
179
186
  //# sourceMappingURL=errors.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,QAAQ,EACxB,cAAc,EACf,6BAA6B;AAG9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AAE7B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAE3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAAK,CAAC;AAC9C,MAAM,CAAC,MAAM,0BAA0B,GAAG,oBAAoB,CAAC;AAU/D,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAU;IAEhB,QAAQ,CAAS;IAEjB,MAAM,CAAU;IAEzB;;;;OAIG;IACH,YAAY,KAAc;QACxB,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG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tB,aAAa,CAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error) ?? undefined;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"]}
1
+ {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,QAAQ,EACxB,cAAc,EACf,6BAA6B;AAO9B,OAAO,EACL,eAAe,EACf,aAAa,EACb,eAAe,EACf,kBAAkB,EACnB,4BAA4B;AA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AU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EACjC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAChC;YACD,KAAK;SACN,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;QAC7C,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACL,eAAe,CACb,UAAU,CAAC,GAAG,CAAC,QAAQ,EACvB,eAAe,CAAC,KAAK,CAAC,EACtB,aAAa,CAAC,KAAK,CAAC,CACrB;QACD,KAAK;KACN,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,oBAAoB,GAA2B,MAAM,CAAC;IACjE,IAAI,EAAE,MAAM,EAAE;IACd,OAAO,EAAE,MAAM,EAAE;IACjB,KAAK,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;IACzB,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAiB,GAAG,EAAE,CAAC,oBAAoB,CAAC,CAAC;CAClE,CAAC,CAAC","sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type {\n SerializedSnapError,\n SnapError,\n TrackableError,\n} from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Struct } from '@metamask/superstruct';\nimport { lazy, nullable, object, string } from '@metamask/superstruct';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error) ?? undefined;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string | null,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack ?? undefined;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // If the JSON-RPC error is double wrapped, unwrap it further to provide the stack.\n if (\n isObject(error.data.cause.data) &&\n isObject(error.data.cause.data.cause) &&\n error.data.cause.message === error.data.cause.data.cause.message\n ) {\n const nestedCause = error.data.cause.data.cause;\n const { code, message } = error.data.cause;\n return [\n getJsonRpcError(code, message, getErrorStack(nestedCause)),\n false,\n ];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n\nexport const TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n"]}
package/dist/types.d.cts CHANGED
@@ -11,8 +11,8 @@ export declare enum NpmSnapFileNames {
11
11
  }
12
12
  export declare const NameStruct: Struct<string, null>;
13
13
  export declare const NpmSnapPackageJsonStruct: Struct<{
14
- version: import("@metamask/utils").SemVerVersion;
15
14
  name: string;
15
+ version: import("@metamask/utils").SemVerVersion;
16
16
  main?: string | undefined;
17
17
  repository?: {
18
18
  type: string;
package/dist/types.d.mts CHANGED
@@ -11,8 +11,8 @@ export declare enum NpmSnapFileNames {
11
11
  }
12
12
  export declare const NameStruct: Struct<string, null>;
13
13
  export declare const NpmSnapPackageJsonStruct: Struct<{
14
- version: import("@metamask/utils").SemVerVersion;
15
14
  name: string;
15
+ version: import("@metamask/utils").SemVerVersion;
16
16
  main?: string | undefined;
17
17
  repository?: {
18
18
  type: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-utils",
3
- "version": "11.2.0",
3
+ "version": "11.3.0",
4
4
  "description": "A collection of utilities for MetaMask Snaps",
5
5
  "keywords": [
6
6
  "MetaMask",