From 91d83a11f18254f14192c1196e23a24a54346aa4 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Wed, 19 Apr 2023 11:20:43 +0200 Subject: [PATCH 1/2] Allow passing unknown values as cause Previously, only the `Error` type as accepted as cause, but since any value can be thrown as error, we have to allow any value to be used. --- src/errors.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/utils.ts | 9 ++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/errors.test.ts b/src/errors.test.ts index 63e0ffe..8398edd 100644 --- a/src/errors.test.ts +++ b/src/errors.test.ts @@ -117,6 +117,25 @@ describe('rpcErrors', () => { }, }); }); + + it('serializes a non-Error-instance cause', () => { + const error = rpcErrors.invalidInput({ + data: { + foo: 'bar', + cause: 'foo', + }, + }); + + const serializedError = error.serialize(); + assert(serializedError.data); + assert(isPlainObject(serializedError.data)); + + expect(serializedError.data.cause).not.toBeInstanceOf(Error); + expect(serializedError.data).toStrictEqual({ + foo: 'bar', + cause: 'foo', + }); + }); }); describe('providerErrors', () => { @@ -168,4 +187,23 @@ describe('providerErrors', () => { }, }); }); + + it('serializes a non-Error-instance cause', () => { + const error = providerErrors.unauthorized({ + data: { + foo: 'bar', + cause: 'foo', + }, + }); + + const serializedError = error.serialize(); + assert(serializedError.data); + assert(isPlainObject(serializedError.data)); + + expect(serializedError.data.cause).not.toBeInstanceOf(Error); + expect(serializedError.data).toStrictEqual({ + foo: 'bar', + cause: 'foo', + }); + }); }); diff --git a/src/utils.ts b/src/utils.ts index 69abc87..6ee6bef 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -13,14 +13,17 @@ import { errorCodes, errorValues } from './error-constants'; * A data object, that must be either: * * - A JSON-serializable object. - * - An object with a `cause` property that is an `Error` instance, and any + * - An object with a `cause` property that is an error-like value, and any * other properties that are JSON-serializable. */ export type DataWithOptionalCause = | Json | { - [key: string]: Json | Error; - cause: Error; + // Unfortunately we can't use just `Json` here, because all properties of + // an object with an index signature must be assignable to the index + // signature's type. So we have to use `Json | unknown` instead. + [key: string]: Json | unknown; + cause: unknown; }; const FALLBACK_ERROR_CODE = errorCodes.rpc.internal; From 2476986f372671fc22246c29f8e70ca56e515d38 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Wed, 19 Apr 2023 11:38:03 +0200 Subject: [PATCH 2/2] Update coverage threshold --- jest.config.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jest.config.js b/jest.config.js index 0ee455d..0acc5bf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,10 +45,10 @@ module.exports = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 94.25, + branches: 94.31, functions: 94.11, - lines: 97.04, - statements: 97.04, + lines: 97.05, + statements: 97.05, }, },