Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing unknown values as cause #91

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ describe('rpcErrors', () => {
},
});
});

it('serializes a non-Error-instance cause', () => {
const error = rpcErrors.invalidInput({
data: {
foo: 'bar',
cause: 'foo',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when this is not JSON-serializable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is used.

},
});

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', () => {
Expand Down Expand Up @@ -169,4 +188,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',
});
});
});
9 changes: 6 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,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;
Expand Down