Skip to content

Commit 88f1100

Browse files
committed
feat: propagate data.cause as cause in JsonRpcError constructor
1 parent 1c1ffa9 commit 88f1100

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/classes.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
Json,
33
JsonRpcError as SerializedJsonRpcError,
44
} from '@metamask/utils';
5-
import { isPlainObject } from '@metamask/utils';
5+
import { hasProperty, isObject, isPlainObject } from '@metamask/utils';
66
import safeStringify from 'fast-safe-stringify';
77

88
import type { OptionalDataWithOptionalCause } from './utils';
@@ -19,6 +19,10 @@ export type { SerializedJsonRpcError };
1919
export class JsonRpcError<
2020
Data extends OptionalDataWithOptionalCause,
2121
> extends Error {
22+
23+
// This can be removed when tsconfig lib and/or target have changed to >=es2022
24+
public cause: OptionalDataWithOptionalCause;
25+
2226
public code: number;
2327

2428
public data?: Data;
@@ -36,6 +40,11 @@ export class JsonRpcError<
3640
this.code = code;
3741
if (data !== undefined) {
3842
this.data = data;
43+
if (isObject(data) && hasProperty(data, 'cause')) {
44+
this.cause = isObject(data.cause)
45+
? data?.['cause']
46+
: undefined;
47+
}
3948
}
4049
}
4150

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"esModuleInterop": true,
44
"exactOptionalPropertyTypes": true,
55
"forceConsistentCasingInFileNames": true,
6+
// Remove custom `cause` field from JsonRpcError when updating
67
"lib": ["ES2020"],
78
"module": "CommonJS",
89
"moduleResolution": "node",

0 commit comments

Comments
 (0)