From ab7215108780b83b68921c012e0173b88760b57d Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Tue, 29 Oct 2024 17:58:29 -0400 Subject: [PATCH] migrate CartoAPIError tests --- test/api/carto-api-error.test.ts | 117 +++++++++++++++++++++++++++++++ test/api/query.test.ts | 2 +- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 test/api/carto-api-error.test.ts diff --git a/test/api/carto-api-error.test.ts b/test/api/carto-api-error.test.ts new file mode 100644 index 0000000..b9fa8f7 --- /dev/null +++ b/test/api/carto-api-error.test.ts @@ -0,0 +1,117 @@ +import {expect, test} from 'vitest'; +import {APIErrorContext, CartoAPIError} from '@carto/api-client'; + +[ + { + title: '400 error', + error: new Error('Bad'), + errorContext: {requestType: 'Map data'}, + response: {status: 400}, + message: ` +Map data API request failed +Server returned: Bad request (400): Bad +`, + }, + { + title: '401 error', + error: new Error('Unauthorized'), + errorContext: {requestType: 'Map data'}, + response: {status: 401}, + message: ` +Map data API request failed +Server returned: Unauthorized access (401): Unauthorized +`, + }, + { + title: '403 error', + error: new Error('Forbidden'), + errorContext: {requestType: 'Map data'}, + response: {status: 403}, + message: ` +Map data API request failed +Server returned: Unauthorized access (403): Forbidden +`, + }, + { + title: '404 error', + error: new Error('Not found'), + errorContext: {requestType: 'Map data'}, + response: {status: 404}, + message: ` +Map data API request failed +Server returned: Not found (404): Not found +`, + }, + { + title: '500 error', + error: new Error('Source error'), + errorContext: {requestType: 'Map data'}, + response: {status: 500}, + message: ` +Map data API request failed +Server returned: Error (500): Source error +`, + }, + { + title: 'Full error context: instantiation', + error: new Error('Source error'), + errorContext: { + requestType: 'Map instantiation', + connection: 'connectionName', + source: 'sourceName', + type: 'query', + }, + response: {status: 500}, + message: ` +Map instantiation API request failed +Server returned: Error (500): Source error +Connection: connectionName +Source: sourceName +Type: query +`, + }, + { + title: 'Full error context: public map', + error: new Error('Source error'), + errorContext: { + requestType: 'Public map', + mapId: 'abcd', + }, + response: {status: 500}, + message: ` +Public map API request failed +Server returned: Error (500): Source error +Map Id: abcd +`, + }, + { + title: 'Full error context: custom value', + error: new Error('Source error'), + errorContext: { + requestType: 'Tile stats', + connection: 'connectionName', + source: 'sourceName', + type: 'tileset', + customKey: 'customValue', + }, + response: {status: 500}, + message: ` +Tile stats API request failed +Server returned: Error (500): Source error +Connection: connectionName +Source: sourceName +Type: tileset +Custom Key: customValue +`, + }, +].forEach(({title, error, errorContext, response, message}) => { + test(`CartoAPIError: ${title}`, () => { + const cartoAPIError = new CartoAPIError( + error, + errorContext as APIErrorContext, + response as Response + ); + expect(cartoAPIError).toBeTruthy(); + expect(cartoAPIError.message).toBe(message.slice(1)); + }); +}); diff --git a/test/api/query.test.ts b/test/api/query.test.ts index f9880ef..fc2d08a 100644 --- a/test/api/query.test.ts +++ b/test/api/query.test.ts @@ -15,7 +15,7 @@ beforeEach(() => { afterEach(() => void vi.restoreAllMocks()); -test('query', async (t) => { +test('query', async () => { const mockFetch = vi.mocked(fetch); const response = await query({