From 20b7920087ef2b7158fc7e14d56aad500e4f03e9 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Wed, 23 Aug 2023 11:11:49 +0000 Subject: [PATCH] types: Make Data type-parameter optional in JsonRpcError This is in order to provide backwards-compatibility with code implemented towards v5.0.0. --- src/classes.ts | 4 ++-- src/utils.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/classes.ts b/src/classes.ts index dbd6429..b7c3540 100644 --- a/src/classes.ts +++ b/src/classes.ts @@ -5,7 +5,7 @@ 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 }; @@ -16,7 +16,7 @@ export type { SerializedJsonRpcError }; * * Permits any integer error code. */ -export class JsonRpcError extends Error { +export class JsonRpcError extends Error { public code: number; public data?: Data; diff --git a/src/utils.ts b/src/utils.ts index 192d7a8..40a4677 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,6 +29,16 @@ 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.';