Skip to content

Commit

Permalink
CARTO: requestWithParameters to store received json response in Carto…
Browse files Browse the repository at this point in the history
…APIError (#8920)
  • Loading branch information
zbigg committed May 24, 2024
1 parent 8629f34 commit a51fc69
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion modules/carto/src/api/carto-api-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ export class CartoAPIError extends Error {
/** Response from server */
response?: Response;

constructor(error: Error, errorContext: APIErrorContext, response?: Response) {
/** JSON Response from server */
responseJson?: any;

constructor(
error: Error,
errorContext: APIErrorContext,
response?: Response,
responseJson?: any
) {
let responseString = 'Failed to connect';
if (response) {
responseString = 'Server returned: ';
Expand Down Expand Up @@ -46,6 +54,7 @@ export class CartoAPIError extends Error {

this.name = 'CartoAPIError';
this.response = response;
this.responseJson = responseJson;
this.error = error;
this.errorContext = errorContext;
}
Expand Down
4 changes: 3 additions & 1 deletion modules/carto/src/api/request-with-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ export async function requestWithParameters<T = any>({
: fetch(url, {headers});

let response: Response | undefined;
let responseJson;
const jsonPromise: Promise<T> = fetchPromise
.then((_response: Response) => {
response = _response;
return response.json();
})
.then((json: any) => {
responseJson = json;
if (!response || !response.ok) {
throw new Error(json.error);
}
return json;
})
.catch((error: Error) => {
REQUEST_CACHE.delete(key);
throw new CartoAPIError(error, errorContext, response);
throw new CartoAPIError(error, errorContext, response, responseJson);
});

REQUEST_CACHE.set(key, jsonPromise);
Expand Down
11 changes: 10 additions & 1 deletion test/modules/carto/api/request-with-parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ test('requestWithParameters#nocacheErrorContext', async t => {
error2 = error as Error;
}

t.deepEquals(
(error1 as CartoAPIError).responseJson,
{error: 'CustomError', customData: {abc: 'def'}},
'responseJson',
'propagates actual JSON error response '
);
t.equals(calls.length, 2, '2 unique requests, failures not cached');
t.true(error1 instanceof CartoAPIError, 'error #1 type');
t.is((error1 as CartoAPIError).errorContext.requestType, 'Map data', 'error #1 context');
Expand All @@ -96,7 +102,10 @@ test('requestWithParameters#nocacheErrorContext', async t => {
},
// @ts-ignore
(url: string, headers: HeadersInit) => {
return Promise.reject(new Error('404 Not Found'));
return Promise.resolve({
ok: false,
json: () => Promise.resolve({error: 'CustomError', customData: {abc: 'def'}})
});
}
);
t.end();
Expand Down

0 comments on commit a51fc69

Please sign in to comment.