Skip to content

Commit

Permalink
fix: improve error reporting for getEvents, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Jan 24, 2023
1 parent e1bfba5 commit ffb00a3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/serverApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ export class FingerprintJsServerApiClient {
if (isEventError(err)) {
throw err;
}
const error = err.toString() === '[object Object]' ? JSON.stringify(err) : err.toString();
throw {
status: 0,
error: new Error(err.toString()),
error: error,
};
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`[Mocked response] Get Event Error with bad shape 1`] = `
Object {
"error": "{\\"error\\":\\"Some text instead og shaped object\\",\\"status\\":404}",
"status": 0,
}
`;

exports[`[Mocked response] Get Event with request_id 1`] = `
Object {
"products": Object {
Expand Down
76 changes: 76 additions & 0 deletions tests/mocked-responses-tests/getEventTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,80 @@ describe('[Mocked response] Get Event', () => {

expect(response).toMatchSnapshot();
});

test('403 error', async () => {
const errorInfo = {
code: 'TokenRequired',
message: 'secret key is required',
};
(fetch as unknown as jest.Mock).mockReturnValue(
Promise.resolve(
new Response(
JSON.stringify({
error: errorInfo,
}),
{
status: 403,
}
)
)
);
await expect(client.getEvent(existingRequestId)).rejects.toMatchObject({
status: 403,
error: errorInfo,
});
});

test('404 error', async () => {
const errorInfo = {
code: 'RequestNotFound',
message: 'request id is not found',
};
(fetch as unknown as jest.Mock).mockReturnValue(
Promise.resolve(
new Response(
JSON.stringify({
error: errorInfo,
}),
{
status: 404,
}
)
)
);
await expect(client.getEvent(existingRequestId)).rejects.toMatchObject({
status: 404,
error: errorInfo,
});
});

test('Error with bad shape', async () => {
const errorInfo = 'Some text instead og shaped object';
(fetch as unknown as jest.Mock).mockReturnValue(
Promise.resolve(
new Response(
JSON.stringify({
error: errorInfo,
}),
{
status: 404,
}
)
)
);
await expect(client.getEvent(existingRequestId)).rejects.toMatchSnapshot();
});

test('Error with bad JSON', async () => {
(fetch as unknown as jest.Mock).mockReturnValue(
Promise.resolve(
new Response('(Some bad JSON)', {
status: 404,
})
)
);
await expect(client.getEvent(existingRequestId)).rejects.toMatchObject({
status: 0,
});
});
});
13 changes: 13 additions & 0 deletions tests/mocked-responses-tests/getVisitorsTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,17 @@ describe('[Mocked response] Get Visitors', () => {
retryAfter: 1,
});
});

test('Error with bad JSON', async () => {
(fetch as unknown as jest.Mock).mockReturnValue(
Promise.resolve(
new Response('(Some bad JSON)', {
status: 404,
})
)
);
await expect(client.getVisitorHistory(existingVisitorId)).rejects.toMatchObject({
status: 0,
});
});
});

0 comments on commit ffb00a3

Please sign in to comment.