Skip to content

Commit

Permalink
feat: propagate data.cause as cause in JsonRpcError constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed May 12, 2024
1 parent 1c1ffa9 commit 88f1100
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -19,6 +19,10 @@ export type { SerializedJsonRpcError };
export class JsonRpcError<
Data extends OptionalDataWithOptionalCause,
> extends Error {

Check failure on line 22 in src/classes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Delete `⏎`
// This can be removed when tsconfig lib and/or target have changed to >=es2022
public cause: OptionalDataWithOptionalCause;

public code: number;

public data?: Data;
Expand All @@ -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)

Check failure on line 44 in src/classes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Replace `⏎··········?·data?.['cause']⏎·········` with `·?·data?.['cause']`
? data?.['cause']

Check failure on line 45 in src/classes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

["cause"] is better written in dot notation
: undefined;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
// Remove custom `cause` field from JsonRpcError when updating
"lib": ["ES2020"],
"module": "CommonJS",
"moduleResolution": "node",
Expand Down

0 comments on commit 88f1100

Please sign in to comment.