Skip to content

Commit

Permalink
test: add cases for cause propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed May 13, 2024
1 parent 6013edf commit e6350af
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/__fixtures__/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { rpcErrors } from '..';

export const dummyData = { foo: 'bar' };
export const dummyMessage = 'baz';
export const dummyData = { foo: 'bar' };
export const dummyDataWithCause = {
foo: 'bar',
cause: { message: dummyMessage },
};

export const invalidError0 = 0;
export const invalidError1 = ['foo', 'bar', 3];
Expand Down
32 changes: 32 additions & 0 deletions src/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { assert, isPlainObject } from '@metamask/utils';
import { rpcErrors, providerErrors, errorCodes } from '.';
import {
dummyData,
dummyDataWithCause,
dummyMessage,
CUSTOM_ERROR_MESSAGE,
SERVER_ERROR_CODE,
CUSTOM_ERROR_CODE,
Expand Down Expand Up @@ -97,6 +99,21 @@ describe('rpcErrors', () => {
},
);

it.each(Object.entries(rpcErrors).filter(([key]) => key !== 'server'))(
'%s propagates data.cause if set',
(key, value) => {
const createError = value as any;
const error = createError({
message: null,
data: Object.assign({}, dummyDataWithCause),
});
// @ts-expect-error TypeScript does not like indexing into this with the key
const rpcCode = errorCodes.rpc[key];
expect(error.message).toBe(getMessageFromCode(rpcCode));
expect(error.cause.message).toBe(dummyMessage);
},
);

it('serializes a cause', () => {
const error = rpcErrors.invalidInput({
data: {
Expand Down Expand Up @@ -156,6 +173,21 @@ describe('providerErrors', () => {
},
);

it.each(Object.entries(providerErrors).filter(([key]) => key !== 'custom'))(
'%s propagates data.cause if set',
(key, value) => {
const createError = value as any;
const error = createError({
message: null,
data: Object.assign({}, dummyDataWithCause),
});
// @ts-expect-error TypeScript does not like indexing into this with the key
const providerCode = errorCodes.provider[key];
expect(error.message).toBe(getMessageFromCode(providerCode));
expect(error.cause.message).toBe(dummyMessage);
},
);

it('custom returns appropriate value', () => {
const error = providerErrors.custom({
code: CUSTOM_ERROR_CODE,
Expand Down

0 comments on commit e6350af

Please sign in to comment.