Skip to content

Commit 6013edf

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

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ module.exports = {
4545
// An object that configures minimum threshold enforcement for coverage results
4646
coverageThreshold: {
4747
global: {
48-
branches: 92.64,
48+
branches: 93.05,
4949
functions: 94.44,
50-
lines: 92.85,
51-
statements: 92.85,
50+
lines: 92.96,
51+
statements: 92.96,
5252
},
5353
},
5454

src/classes.ts

Lines changed: 11 additions & 1 deletion
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,9 @@ export type { SerializedJsonRpcError };
1919
export class JsonRpcError<
2020
Data extends OptionalDataWithOptionalCause,
2121
> extends Error {
22+
// This can be removed when tsconfig lib and/or target have changed to >=es2022
23+
public cause: OptionalDataWithOptionalCause;
24+
2225
public code: number;
2326

2427
public data?: Data;
@@ -36,6 +39,13 @@ export class JsonRpcError<
3639
this.code = code;
3740
if (data !== undefined) {
3841
this.data = data;
42+
if (
43+
isObject(data) &&
44+
hasProperty(data, 'cause') &&
45+
isObject(data.cause)
46+
) {
47+
this.cause = data.cause;
48+
}
3949
}
4050
}
4151

tsconfig.json

Lines changed: 1 addition & 0 deletions
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)