diff --git a/src/classes.ts b/src/classes.ts index 624dd8f..0162bed 100644 --- a/src/classes.ts +++ b/src/classes.ts @@ -2,7 +2,7 @@ import type { Json, JsonRpcError as SerializedJsonRpcError, } from '@metamask/utils'; -import { isPlainObject } from '@metamask/utils'; +import { hasProperty, isObject, isPlainObject } from '@metamask/utils'; import safeStringify from 'fast-safe-stringify'; import type { OptionalDataWithOptionalCause } from './utils'; @@ -19,6 +19,10 @@ export type { SerializedJsonRpcError }; export class JsonRpcError< Data extends OptionalDataWithOptionalCause, > extends Error { + + // This can be removed when tsconfig lib and/or target have changed to >=es2022 + public cause: OptionalDataWithOptionalCause; + public code: number; public data?: Data; @@ -36,6 +40,11 @@ export class JsonRpcError< this.code = code; if (data !== undefined) { this.data = data; + if (isObject(data) && hasProperty(data, 'cause')) { + this.cause = isObject(data.cause) + ? data?.['cause'] + : undefined; + } } } diff --git a/tsconfig.json b/tsconfig.json index 56f6531..b49529a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "esModuleInterop": true, "exactOptionalPropertyTypes": true, "forceConsistentCasingInFileNames": true, + // Remove custom `cause` field from JsonRpcError when updating "lib": ["ES2020"], "module": "CommonJS", "moduleResolution": "node",