Skip to content

Commit

Permalink
types: Make Data type-parameter optional in JsonRpcError
Browse files Browse the repository at this point in the history
This is in order to provide backwards-compatibility with code
implemented towards v5.0.0.
  • Loading branch information
legobeat committed Aug 23, 2023
1 parent ab0ebca commit aa42de1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type {
import { isPlainObject } from '@metamask/utils';
import safeStringify from 'fast-safe-stringify';

import type { DataWithOptionalCause } from './utils';
import type {
DataWithOptionalCause,
OptionalDataWithOptionalCause,
} from './utils';
import { serializeCause } from './utils';

export type { SerializedJsonRpcError };
Expand All @@ -16,7 +19,9 @@ export type { SerializedJsonRpcError };
*
* Permits any integer error code.
*/
export class JsonRpcError<Data extends DataWithOptionalCause> extends Error {
export class JsonRpcError<
Data extends OptionalDataWithOptionalCause,
> extends Error {
public code: number;

public data?: Data;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export type DataWithOptionalCause =
cause: unknown;
};

/**
* A data object, that must be either:
*
* - A valid DataWithOptionalCause value.
* - undefined.
*/
export type OptionalDataWithOptionalCause = undefined | DataWithOptionalCause;

const FALLBACK_ERROR_CODE = errorCodes.rpc.internal;
const FALLBACK_MESSAGE =
'Unspecified error message. This is a bug, please report it.';
Expand Down

0 comments on commit aa42de1

Please sign in to comment.