Skip to content

Commit e06f8d8

Browse files
Mrtenzlegobeat
andauthored
Allow passing unknown values as cause (#91)
* 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. * Update coverage threshold --------- Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
1 parent 1d1852d commit e06f8d8

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
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: 94.25,
48+
branches: 94.31,
4949
functions: 94.11,
50-
lines: 97.04,
51-
statements: 97.04,
50+
lines: 97.05,
51+
statements: 97.05,
5252
},
5353
},
5454

src/errors.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ describe('rpcErrors', () => {
118118
},
119119
});
120120
});
121+
122+
it('serializes a non-Error-instance cause', () => {
123+
const error = rpcErrors.invalidInput({
124+
data: {
125+
foo: 'bar',
126+
cause: 'foo',
127+
},
128+
});
129+
130+
const serializedError = error.serialize();
131+
assert(serializedError.data);
132+
assert(isPlainObject(serializedError.data));
133+
134+
expect(serializedError.data.cause).not.toBeInstanceOf(Error);
135+
expect(serializedError.data).toStrictEqual({
136+
foo: 'bar',
137+
cause: 'foo',
138+
});
139+
});
121140
});
122141

123142
describe('providerErrors', () => {
@@ -169,4 +188,23 @@ describe('providerErrors', () => {
169188
},
170189
});
171190
});
191+
192+
it('serializes a non-Error-instance cause', () => {
193+
const error = providerErrors.unauthorized({
194+
data: {
195+
foo: 'bar',
196+
cause: 'foo',
197+
},
198+
});
199+
200+
const serializedError = error.serialize();
201+
assert(serializedError.data);
202+
assert(isPlainObject(serializedError.data));
203+
204+
expect(serializedError.data.cause).not.toBeInstanceOf(Error);
205+
expect(serializedError.data).toStrictEqual({
206+
foo: 'bar',
207+
cause: 'foo',
208+
});
209+
});
172210
});

src/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ import { errorCodes, errorValues } from './error-constants';
1414
* A data object, that must be either:
1515
*
1616
* - A JSON-serializable object.
17-
* - An object with a `cause` property that is an `Error` instance, and any
17+
* - An object with a `cause` property that is an error-like value, and any
1818
* other properties that are JSON-serializable.
1919
*/
2020
export type DataWithOptionalCause =
2121
| Json
2222
| {
23-
[key: string]: Json | Error;
24-
cause: Error;
23+
// Unfortunately we can't use just `Json` here, because all properties of
24+
// an object with an index signature must be assignable to the index
25+
// signature's type. So we have to use `Json | unknown` instead.
26+
[key: string]: Json | unknown;
27+
cause: unknown;
2528
};
2629

2730
const FALLBACK_ERROR_CODE = errorCodes.rpc.internal;

0 commit comments

Comments
 (0)