Skip to content

Commit

Permalink
migrate CartoAPIError tests
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Oct 29, 2024
1 parent dc718da commit ab72151
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
117 changes: 117 additions & 0 deletions test/api/carto-api-error.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
2 changes: 1 addition & 1 deletion test/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit ab72151

Please sign in to comment.