@metamask-previews/keyring-controller 27.0.0-preview-1b0db3500 → 27.0.0-preview-bb11942f2

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,10 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add `isKeyringControllerError` predicate ([#9095](https://github.com/MetaMask/core/pull/9095))
13
+
10
14
  ### Changed
11
15
 
12
16
  - Bump `@metamask/utils` from `^11.9.0` to `^11.11.0` ([#9074](https://github.com/MetaMask/core/pull/9074))
13
17
 
18
+ ### Fixed
19
+
20
+ - Remove use of `instanceof` for `isKeyringNotFoundError` ([#9095](https://github.com/MetaMask/core/pull/9095))
21
+ - Using `instanceof` causes a lot of issue if we have 2 major `@metamask/keyring-controller` major versions in the dependency tree, `class KeyringControllerError` could be different classes and this, making the check to fail.
22
+
14
23
  ## [27.0.0]
15
24
 
16
25
  ### Changed
package/dist/errors.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isKeyringNotFoundError = exports.KeyringControllerError = void 0;
3
+ exports.isKeyringNotFoundError = exports.isKeyringControllerError = exports.KeyringControllerError = void 0;
4
4
  const constants_1 = require("./constants.cjs");
5
5
  /**
6
6
  * Error class for KeyringController-related errors.
@@ -81,6 +81,23 @@ class KeyringControllerError extends Error {
81
81
  }
82
82
  }
83
83
  exports.KeyringControllerError = KeyringControllerError;
84
+ /**
85
+ * Returns `true` if the error is a `KeyringControllerError`.
86
+ *
87
+ * Uses duck-typing on `error.name` rather than `instanceof` so that the check
88
+ * remains correct when multiple major versions of `@metamask/keyring-controller`
89
+ * coexist in the dependency tree (different versions produce different classes,
90
+ * so `instanceof` would return `false` for errors from another version).
91
+ *
92
+ * @param error - The value to check.
93
+ * @returns Whether the error is a `KeyringControllerError`.
94
+ */
95
+ function isKeyringControllerError(error) {
96
+ return (typeof error === 'object' &&
97
+ error !== null &&
98
+ error.name === 'KeyringControllerError');
99
+ }
100
+ exports.isKeyringControllerError = isKeyringControllerError;
84
101
  /**
85
102
  * Returns `true` if the error is a `KeyringNotFound` error thrown by
86
103
  * `KeyringController:withKeyring`. Use this to distinguish a missing keyring
@@ -90,7 +107,7 @@ exports.KeyringControllerError = KeyringControllerError;
90
107
  * @returns Whether the error is a `KeyringNotFound` error.
91
108
  */
92
109
  function isKeyringNotFoundError(error) {
93
- return (error instanceof KeyringControllerError &&
110
+ return (isKeyringControllerError(error) &&
94
111
  error.message === constants_1.KeyringControllerErrorMessage.KeyringNotFound);
95
112
  }
96
113
  exports.isKeyringNotFoundError = isKeyringNotFoundError;
@@ -1 +1 @@
1
- {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,+CAA4D;AAuB5D;;;;;;;;GAQG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAsB/C;;;;;OAKG;IACH,YACE,OAAe,EACf,OAA+C;QAE/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QAErC,2FAA2F;QAC3F,MAAM,KAAK,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC;QAClE,MAAM,IAAI,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC;QAClE,MAAM,OAAO,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC;QAExE,0DAA0D;QAC1D,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACnB,qDAAqD;YACrD,IAAI,CAAC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AO,CACL,KAAK,YAAY,sBAAsB;QACvC,KAAK,CAAC,OAAO,KAAK,yCAA6B,CAAC,eAAe,CAChE,CAAC;AACJ,CAAC;AAPD,wDAOC","sourcesContent":["import { KeyringControllerErrorMessage } from './constants';\n\n/**\n * Options for creating a KeyringControllerError.\n */\nexport type KeyringControllerErrorOptions = {\n /**\n * The underlying error that caused this error (for error chaining).\n * Uses the standard Error.cause property (ES2022).\n */\n cause?: Error;\n /**\n * Optional error code for programmatic error handling.\n * This can be used to identify specific error types without string matching.\n */\n code?: string;\n /**\n * Additional context data associated with the error.\n * Useful for debugging and error reporting.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Error class for KeyringController-related errors.\n *\n * This error class extends the standard Error class and supports:\n * - Error chaining via the `cause` property (ES2022 standard)\n * - Optional error codes for programmatic error handling\n * - Additional context data for debugging\n * - Backward compatibility with the legacy `originalError` property\n */\nexport class KeyringControllerError extends Error {\n /**\n * Optional error code for programmatic error handling.\n */\n code?: string;\n\n /**\n * Additional context data associated with the error.\n */\n context?: Record<string, unknown>;\n\n /**\n * The underlying error that caused this error (ES2022 standard).\n * This is set manually for compatibility with older TypeScript versions.\n */\n cause?: Error;\n\n /**\n * @deprecated Use `cause` instead. This property is maintained for backward compatibility.\n */\n originalError?: Error;\n\n /**\n * Creates a new KeyringControllerError.\n *\n * @param message - The error message.\n * @param options - Error options or an Error object for backward compatibility.\n */\n constructor(\n message: string,\n options?: KeyringControllerErrorOptions | Error,\n ) {\n super(message);\n this.name = 'KeyringControllerError';\n\n // Support both new signature (options object) and legacy signature (Error as second param)\n const cause = options instanceof Error ? options : options?.cause;\n const code = options instanceof Error ? undefined : options?.code;\n const context = options instanceof Error ? undefined : options?.context;\n\n // Set cause property for error chaining (ES2022 standard)\n if (cause) {\n this.cause = cause;\n // Maintain backward compatibility with originalError\n this.originalError = cause;\n }\n\n // Set code and data if provided\n if (code) {\n this.code = code;\n }\n if (context) {\n this.context = context;\n }\n\n // Ensure proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, KeyringControllerError.prototype);\n }\n\n /**\n * Returns a JSON representation of the error.\n * Useful for logging and error reporting.\n *\n * @returns JSON representation of the error.\n */\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n context: this.context,\n stack: this.stack,\n cause: this.cause\n ? {\n name: this.cause.name,\n message: this.cause.message,\n stack: this.cause.stack,\n }\n : undefined,\n };\n }\n\n /**\n * Returns a string representation of the error chain.\n * Includes all chained errors for better debugging.\n *\n * @returns String representation of the error chain.\n */\n toString(): string {\n let result = `${this.name}: ${this.message}`;\n\n if (this.code) {\n result += ` [${this.code}]`;\n }\n\n if (this.cause) {\n result += `\\n Caused by: ${this.cause}`;\n }\n\n return result;\n }\n}\n\n/**\n * Returns `true` if the error is a `KeyringNotFound` error thrown by\n * `KeyringController:withKeyring`. Use this to distinguish a missing keyring\n * from other failures and apply fallback logic.\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringNotFound` error.\n */\nexport function isKeyringNotFoundError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n error instanceof KeyringControllerError &&\n error.message === KeyringControllerErrorMessage.KeyringNotFound\n );\n}\n"]}
1
+ {"version":3,"file":"errors.cjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,+CAA4D;AAuB5D;;;;;;;;GAQG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAsB/C;;;;;OAKG;IACH,YACE,OAAe,EACf,OAA+C;QAE/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QAErC,2FAA2F;QAC3F,MAAM,KAAK,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC;QAClE,MAAM,IAAI,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,C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B,CAAC;QAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,IAAI,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AArGD,wDAqGC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,wBAAwB,CACtC,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,wBAAwB,CAChE,CAAC;AACJ,CAAC;AARD,4DAQC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAc;IAEd,OAAO,CACL,wBAAwB,CAAC,KAAK,CAAC;QAC/B,KAAK,CAAC,OAAO,KAAK,yCAA6B,CAAC,eAAe,CAChE,CAAC;AACJ,CAAC;AAPD,wDAOC","sourcesContent":["import { KeyringControllerErrorMessage } from './constants';\n\n/**\n * Options for creating a KeyringControllerError.\n */\nexport type KeyringControllerErrorOptions = {\n /**\n * The underlying error that caused this error (for error chaining).\n * Uses the standard Error.cause property (ES2022).\n */\n cause?: Error;\n /**\n * Optional error code for programmatic error handling.\n * This can be used to identify specific error types without string matching.\n */\n code?: string;\n /**\n * Additional context data associated with the error.\n * Useful for debugging and error reporting.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Error class for KeyringController-related errors.\n *\n * This error class extends the standard Error class and supports:\n * - Error chaining via the `cause` property (ES2022 standard)\n * - Optional error codes for programmatic error handling\n * - Additional context data for debugging\n * - Backward compatibility with the legacy `originalError` property\n */\nexport class KeyringControllerError extends Error {\n /**\n * Optional error code for programmatic error handling.\n */\n code?: string;\n\n /**\n * Additional context data associated with the error.\n */\n context?: Record<string, unknown>;\n\n /**\n * The underlying error that caused this error (ES2022 standard).\n * This is set manually for compatibility with older TypeScript versions.\n */\n cause?: Error;\n\n /**\n * @deprecated Use `cause` instead. This property is maintained for backward compatibility.\n */\n originalError?: Error;\n\n /**\n * Creates a new KeyringControllerError.\n *\n * @param message - The error message.\n * @param options - Error options or an Error object for backward compatibility.\n */\n constructor(\n message: string,\n options?: KeyringControllerErrorOptions | Error,\n ) {\n super(message);\n this.name = 'KeyringControllerError';\n\n // Support both new signature (options object) and legacy signature (Error as second param)\n const cause = options instanceof Error ? options : options?.cause;\n const code = options instanceof Error ? undefined : options?.code;\n const context = options instanceof Error ? undefined : options?.context;\n\n // Set cause property for error chaining (ES2022 standard)\n if (cause) {\n this.cause = cause;\n // Maintain backward compatibility with originalError\n this.originalError = cause;\n }\n\n // Set code and data if provided\n if (code) {\n this.code = code;\n }\n if (context) {\n this.context = context;\n }\n\n // Ensure proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, KeyringControllerError.prototype);\n }\n\n /**\n * Returns a JSON representation of the error.\n * Useful for logging and error reporting.\n *\n * @returns JSON representation of the error.\n */\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n context: this.context,\n stack: this.stack,\n cause: this.cause\n ? {\n name: this.cause.name,\n message: this.cause.message,\n stack: this.cause.stack,\n }\n : undefined,\n };\n }\n\n /**\n * Returns a string representation of the error chain.\n * Includes all chained errors for better debugging.\n *\n * @returns String representation of the error chain.\n */\n toString(): string {\n let result = `${this.name}: ${this.message}`;\n\n if (this.code) {\n result += ` [${this.code}]`;\n }\n\n if (this.cause) {\n result += `\\n Caused by: ${this.cause}`;\n }\n\n return result;\n }\n}\n\n/**\n * Returns `true` if the error is a `KeyringControllerError`.\n *\n * Uses duck-typing on `error.name` rather than `instanceof` so that the check\n * remains correct when multiple major versions of `@metamask/keyring-controller`\n * coexist in the dependency tree (different versions produce different classes,\n * so `instanceof` would return `false` for errors from another version).\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringControllerError`.\n */\nexport function isKeyringControllerError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as { name?: unknown }).name === 'KeyringControllerError'\n );\n}\n\n/**\n * Returns `true` if the error is a `KeyringNotFound` error thrown by\n * `KeyringController:withKeyring`. Use this to distinguish a missing keyring\n * from other failures and apply fallback logic.\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringNotFound` error.\n */\nexport function isKeyringNotFoundError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n isKeyringControllerError(error) &&\n error.message === KeyringControllerErrorMessage.KeyringNotFound\n );\n}\n"]}
package/dist/errors.d.cts CHANGED
@@ -67,6 +67,18 @@ export declare class KeyringControllerError extends Error {
67
67
  */
68
68
  toString(): string;
69
69
  }
70
+ /**
71
+ * Returns `true` if the error is a `KeyringControllerError`.
72
+ *
73
+ * Uses duck-typing on `error.name` rather than `instanceof` so that the check
74
+ * remains correct when multiple major versions of `@metamask/keyring-controller`
75
+ * coexist in the dependency tree (different versions produce different classes,
76
+ * so `instanceof` would return `false` for errors from another version).
77
+ *
78
+ * @param error - The value to check.
79
+ * @returns Whether the error is a `KeyringControllerError`.
80
+ */
81
+ export declare function isKeyringControllerError(error: unknown): error is KeyringControllerError;
70
82
  /**
71
83
  * Returns `true` if the error is a `KeyringNotFound` error thrown by
72
84
  * `KeyringController:withKeyring`. Use this to distinguish a missing keyring
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.cts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IACd;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,CAAC;AAEF;;;;;;;;GAQG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO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AAgB,wBAAwB,CACtC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,sBAAsB,CAMjC;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,sBAAsB,CAKjC"}
package/dist/errors.d.mts CHANGED
@@ -67,6 +67,18 @@ export declare class KeyringControllerError extends Error {
67
67
  */
68
68
  toString(): string;
69
69
  }
70
+ /**
71
+ * Returns `true` if the error is a `KeyringControllerError`.
72
+ *
73
+ * Uses duck-typing on `error.name` rather than `instanceof` so that the check
74
+ * remains correct when multiple major versions of `@metamask/keyring-controller`
75
+ * coexist in the dependency tree (different versions produce different classes,
76
+ * so `instanceof` would return `false` for errors from another version).
77
+ *
78
+ * @param error - The value to check.
79
+ * @returns Whether the error is a `KeyringControllerError`.
80
+ */
81
+ export declare function isKeyringControllerError(error: unknown): error is KeyringControllerError;
70
82
  /**
71
83
  * Returns `true` if the error is a `KeyringNotFound` error thrown by
72
84
  * `KeyringController:withKeyring`. Use this to distinguish a missing keyring
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.mts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IACd;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,CAAC;AAEF;;;;;;;;GAQG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAElC;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IAEd;;OAEG;IACH,aAAa,CAAC,EAAE,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gB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,sBAAsB,CAKjC"}
package/dist/errors.mjs CHANGED
@@ -77,6 +77,22 @@ export class KeyringControllerError extends Error {
77
77
  return result;
78
78
  }
79
79
  }
80
+ /**
81
+ * Returns `true` if the error is a `KeyringControllerError`.
82
+ *
83
+ * Uses duck-typing on `error.name` rather than `instanceof` so that the check
84
+ * remains correct when multiple major versions of `@metamask/keyring-controller`
85
+ * coexist in the dependency tree (different versions produce different classes,
86
+ * so `instanceof` would return `false` for errors from another version).
87
+ *
88
+ * @param error - The value to check.
89
+ * @returns Whether the error is a `KeyringControllerError`.
90
+ */
91
+ export function isKeyringControllerError(error) {
92
+ return (typeof error === 'object' &&
93
+ error !== null &&
94
+ error.name === 'KeyringControllerError');
95
+ }
80
96
  /**
81
97
  * Returns `true` if the error is a `KeyringNotFound` error thrown by
82
98
  * `KeyringController:withKeyring`. Use this to distinguish a missing keyring
@@ -86,7 +102,7 @@ export class KeyringControllerError extends Error {
86
102
  * @returns Whether the error is a `KeyringNotFound` error.
87
103
  */
88
104
  export function isKeyringNotFoundError(error) {
89
- return (error instanceof KeyringControllerError &&
105
+ return (isKeyringControllerError(error) &&
90
106
  error.message === KeyringControllerErrorMessage.KeyringNotFound);
91
107
  }
92
108
  //# sourceMappingURL=errors.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,wBAAoB;AAuB5D;;;;;;;;GAQG;AACH,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAsB/C;;;;;OAKG;IACH,YACE,OAAe,EACf,OAA+C;QAE/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;QAErC,2FAA2F;QAC3F,MAAM,KAAK,GAAG,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC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AK,IAAI,CAAC,OAAO,EAAE,CAAC;QAE7C,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,IAAI,CAAC,IAAI,GAAG,CAAC;QAC9B,CAAC;QAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,IAAI,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAc;IAEd,OAAO,CACL,KAAK,YAAY,sBAAsB;QACvC,KAAK,CAAC,OAAO,KAAK,6BAA6B,CAAC,eAAe,CAChE,CAAC;AACJ,CAAC","sourcesContent":["import { KeyringControllerErrorMessage } from './constants';\n\n/**\n * Options for creating a KeyringControllerError.\n */\nexport type KeyringControllerErrorOptions = {\n /**\n * The underlying error that caused this error (for error chaining).\n * Uses the standard Error.cause property (ES2022).\n */\n cause?: Error;\n /**\n * Optional error code for programmatic error handling.\n * This can be used to identify specific error types without string matching.\n */\n code?: string;\n /**\n * Additional context data associated with the error.\n * Useful for debugging and error reporting.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Error class for KeyringController-related errors.\n *\n * This error class extends the standard Error class and supports:\n * - Error chaining via the `cause` property (ES2022 standard)\n * - Optional error codes for programmatic error handling\n * - Additional context data for debugging\n * - Backward compatibility with the legacy `originalError` property\n */\nexport class KeyringControllerError extends Error {\n /**\n * Optional error code for programmatic error handling.\n */\n code?: string;\n\n /**\n * Additional context data associated with the error.\n */\n context?: Record<string, unknown>;\n\n /**\n * The underlying error that caused this error (ES2022 standard).\n * This is set manually for compatibility with older TypeScript versions.\n */\n cause?: Error;\n\n /**\n * @deprecated Use `cause` instead. This property is maintained for backward compatibility.\n */\n originalError?: Error;\n\n /**\n * Creates a new KeyringControllerError.\n *\n * @param message - The error message.\n * @param options - Error options or an Error object for backward compatibility.\n */\n constructor(\n message: string,\n options?: KeyringControllerErrorOptions | Error,\n ) {\n super(message);\n this.name = 'KeyringControllerError';\n\n // Support both new signature (options object) and legacy signature (Error as second param)\n const cause = options instanceof Error ? options : options?.cause;\n const code = options instanceof Error ? undefined : options?.code;\n const context = options instanceof Error ? undefined : options?.context;\n\n // Set cause property for error chaining (ES2022 standard)\n if (cause) {\n this.cause = cause;\n // Maintain backward compatibility with originalError\n this.originalError = cause;\n }\n\n // Set code and data if provided\n if (code) {\n this.code = code;\n }\n if (context) {\n this.context = context;\n }\n\n // Ensure proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, KeyringControllerError.prototype);\n }\n\n /**\n * Returns a JSON representation of the error.\n * Useful for logging and error reporting.\n *\n * @returns JSON representation of the error.\n */\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n context: this.context,\n stack: this.stack,\n cause: this.cause\n ? {\n name: this.cause.name,\n message: this.cause.message,\n stack: this.cause.stack,\n }\n : undefined,\n };\n }\n\n /**\n * Returns a string representation of the error chain.\n * Includes all chained errors for better debugging.\n *\n * @returns String representation of the error chain.\n */\n toString(): string {\n let result = `${this.name}: ${this.message}`;\n\n if (this.code) {\n result += ` [${this.code}]`;\n }\n\n if (this.cause) {\n result += `\\n Caused by: ${this.cause}`;\n }\n\n return result;\n }\n}\n\n/**\n * Returns `true` if the error is a `KeyringNotFound` error thrown by\n * `KeyringController:withKeyring`. Use this to distinguish a missing keyring\n * from other failures and apply fallback logic.\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringNotFound` error.\n */\nexport function isKeyringNotFoundError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n error instanceof KeyringControllerError &&\n error.message === KeyringControllerErrorMessage.KeyringNotFound\n );\n}\n"]}
1
+ {"version":3,"file":"errors.mjs","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,wBAAoB;AAuB5D;;;;;;;;GAQG;AACH,MAAM,OAAO,sBA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,IAAI;oBACrB,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO;oBAC3B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK;iBACxB;gBACH,CAAC,CAAC,SAAS;SACd,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,QAAQ;QACN,IAAI,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;QAE7C,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,IAAI,CAAC,IAAI,GAAG,CAAC;QAC9B,CAAC;QAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,IAAI,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CACtC,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,wBAAwB,CAChE,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAc;IAEd,OAAO,CACL,wBAAwB,CAAC,KAAK,CAAC;QAC/B,KAAK,CAAC,OAAO,KAAK,6BAA6B,CAAC,eAAe,CAChE,CAAC;AACJ,CAAC","sourcesContent":["import { KeyringControllerErrorMessage } from './constants';\n\n/**\n * Options for creating a KeyringControllerError.\n */\nexport type KeyringControllerErrorOptions = {\n /**\n * The underlying error that caused this error (for error chaining).\n * Uses the standard Error.cause property (ES2022).\n */\n cause?: Error;\n /**\n * Optional error code for programmatic error handling.\n * This can be used to identify specific error types without string matching.\n */\n code?: string;\n /**\n * Additional context data associated with the error.\n * Useful for debugging and error reporting.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Error class for KeyringController-related errors.\n *\n * This error class extends the standard Error class and supports:\n * - Error chaining via the `cause` property (ES2022 standard)\n * - Optional error codes for programmatic error handling\n * - Additional context data for debugging\n * - Backward compatibility with the legacy `originalError` property\n */\nexport class KeyringControllerError extends Error {\n /**\n * Optional error code for programmatic error handling.\n */\n code?: string;\n\n /**\n * Additional context data associated with the error.\n */\n context?: Record<string, unknown>;\n\n /**\n * The underlying error that caused this error (ES2022 standard).\n * This is set manually for compatibility with older TypeScript versions.\n */\n cause?: Error;\n\n /**\n * @deprecated Use `cause` instead. This property is maintained for backward compatibility.\n */\n originalError?: Error;\n\n /**\n * Creates a new KeyringControllerError.\n *\n * @param message - The error message.\n * @param options - Error options or an Error object for backward compatibility.\n */\n constructor(\n message: string,\n options?: KeyringControllerErrorOptions | Error,\n ) {\n super(message);\n this.name = 'KeyringControllerError';\n\n // Support both new signature (options object) and legacy signature (Error as second param)\n const cause = options instanceof Error ? options : options?.cause;\n const code = options instanceof Error ? undefined : options?.code;\n const context = options instanceof Error ? undefined : options?.context;\n\n // Set cause property for error chaining (ES2022 standard)\n if (cause) {\n this.cause = cause;\n // Maintain backward compatibility with originalError\n this.originalError = cause;\n }\n\n // Set code and data if provided\n if (code) {\n this.code = code;\n }\n if (context) {\n this.context = context;\n }\n\n // Ensure proper prototype chain for instanceof checks\n Object.setPrototypeOf(this, KeyringControllerError.prototype);\n }\n\n /**\n * Returns a JSON representation of the error.\n * Useful for logging and error reporting.\n *\n * @returns JSON representation of the error.\n */\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n context: this.context,\n stack: this.stack,\n cause: this.cause\n ? {\n name: this.cause.name,\n message: this.cause.message,\n stack: this.cause.stack,\n }\n : undefined,\n };\n }\n\n /**\n * Returns a string representation of the error chain.\n * Includes all chained errors for better debugging.\n *\n * @returns String representation of the error chain.\n */\n toString(): string {\n let result = `${this.name}: ${this.message}`;\n\n if (this.code) {\n result += ` [${this.code}]`;\n }\n\n if (this.cause) {\n result += `\\n Caused by: ${this.cause}`;\n }\n\n return result;\n }\n}\n\n/**\n * Returns `true` if the error is a `KeyringControllerError`.\n *\n * Uses duck-typing on `error.name` rather than `instanceof` so that the check\n * remains correct when multiple major versions of `@metamask/keyring-controller`\n * coexist in the dependency tree (different versions produce different classes,\n * so `instanceof` would return `false` for errors from another version).\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringControllerError`.\n */\nexport function isKeyringControllerError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n typeof error === 'object' &&\n error !== null &&\n (error as { name?: unknown }).name === 'KeyringControllerError'\n );\n}\n\n/**\n * Returns `true` if the error is a `KeyringNotFound` error thrown by\n * `KeyringController:withKeyring`. Use this to distinguish a missing keyring\n * from other failures and apply fallback logic.\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringNotFound` error.\n */\nexport function isKeyringNotFoundError(\n error: unknown,\n): error is KeyringControllerError {\n return (\n isKeyringControllerError(error) &&\n error.message === KeyringControllerErrorMessage.KeyringNotFound\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/keyring-controller",
3
- "version": "27.0.0-preview-1b0db3500",
3
+ "version": "27.0.0-preview-bb11942f2",
4
4
  "description": "Stores identities seen in the wallet and manages interactions such as signing",
5
5
  "keywords": [
6
6
  "Ethereum",