diff --git a/src/serverApiClient.ts b/src/serverApiClient.ts index e58c9b53..1e472635 100644 --- a/src/serverApiClient.ts +++ b/src/serverApiClient.ts @@ -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, }; }); } diff --git a/tests/mocked-responses-tests/__snapshots__/getEventTests.spec.ts.snap b/tests/mocked-responses-tests/__snapshots__/getEventTests.spec.ts.snap index a791a684..a9fc0445 100644 --- a/tests/mocked-responses-tests/__snapshots__/getEventTests.spec.ts.snap +++ b/tests/mocked-responses-tests/__snapshots__/getEventTests.spec.ts.snap @@ -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 { diff --git a/tests/mocked-responses-tests/getEventTests.spec.ts b/tests/mocked-responses-tests/getEventTests.spec.ts index 96dba592..4a6bc8cf 100644 --- a/tests/mocked-responses-tests/getEventTests.spec.ts +++ b/tests/mocked-responses-tests/getEventTests.spec.ts @@ -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, + }); + }); }); diff --git a/tests/mocked-responses-tests/getVisitorsTests.spec.ts b/tests/mocked-responses-tests/getVisitorsTests.spec.ts index 816c5e60..a208bd1f 100644 --- a/tests/mocked-responses-tests/getVisitorsTests.spec.ts +++ b/tests/mocked-responses-tests/getVisitorsTests.spec.ts @@ -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, + }); + }); });