Skip to content

Commit

Permalink
feat: parsing body for failed ajax responses (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Jul 18, 2023
1 parent 5eafa1f commit 7a875ef
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-insects-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ajax': minor
---

Parses response body automatically for fetchJson failed responses. Add test for reading out the response body in cases of a failed response in regular fetch call.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 39 additions & 8 deletions packages/ajax/src/Ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ export class Ajax {
*
* @param {RequestInfo} info
* @param {RequestInit & Partial<CacheRequestExtension>} [init]
* @param {Boolean} [parseErrorResponse]
* @returns {Promise<Response>}
*/
async fetch(info, init) {
async fetch(info, init, parseErrorResponse = false) {
const request = /** @type {CacheRequest} */ (new Request(info, { ...init }));
request.cacheOptions = init?.cacheOptions;
request.params = init?.params;
Expand All @@ -137,7 +138,11 @@ export class Ajax {
const response = /** @type {CacheResponse} */ (interceptedRequestOrResponse);
response.request = request;
if (isFailedResponse(interceptedRequestOrResponse)) {
throw new AjaxFetchError(request, response);
throw new AjaxFetchError(
request,
response,
parseErrorResponse ? await this.__attemptParseFailedResponseBody(response) : undefined,
);
}
// prevent network request, return cached response
return response;
Expand All @@ -149,7 +154,11 @@ export class Ajax {
const interceptedResponse = await this.__interceptResponse(response);

if (isFailedResponse(interceptedResponse)) {
throw new AjaxFetchError(request, interceptedResponse);
throw new AjaxFetchError(
request,
response,
parseErrorResponse ? await this.__attemptParseFailedResponseBody(response) : undefined,
);
}
return interceptedResponse;
}
Expand All @@ -163,8 +172,7 @@ export class Ajax {
*
* @param {RequestInfo} info
* @param {LionRequestInit} [init]
* @template T
* @returns {Promise<{ response: Response, body: T }>}
* @returns {Promise<{ response: Response, body: string|Object }>}
*/
async fetchJson(info, init) {
const lionInit = {
Expand All @@ -183,15 +191,25 @@ export class Ajax {

// typecast LionRequestInit back to RequestInit
const jsonInit = /** @type {RequestInit} */ (lionInit);
const response = await this.fetch(info, jsonInit);
const response = await this.fetch(info, jsonInit, true);

const body = await this.__parseBody(response);

return { response, body };
}

/**
* @param {Response} response
* @returns {Promise<string|Object>}
*/
async __parseBody(response) {
let responseText = await response.text();

const { jsonPrefix } = this.__config;
if (typeof jsonPrefix === 'string' && responseText.startsWith(jsonPrefix)) {
responseText = responseText.substring(jsonPrefix.length);
}

/** @type {any} */
let body = responseText;

if (
Expand All @@ -207,8 +225,21 @@ export class Ajax {
} else {
body = responseText;
}
return body;
}

return { response, body };
/**
* @param {Response} response
* @returns {Promise<string|Object|undefined>}
*/
async __attemptParseFailedResponseBody(response) {
let body;
try {
body = await this.__parseBody(response);
} catch (e) {
// no need to throw/log, failed responses often don't have a body
}
return body;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/ajax/src/AjaxFetchError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ export class AjaxFetchError extends Error {
/**
* @param {Request} request
* @param {Response} response
* @param {string|Object} [body]
*/
constructor(request, response) {
constructor(request, response, body) {
super(`Fetch request to ${request.url} failed.`);
this.request = request;
this.response = response;
this.body = body;
}
}
37 changes: 37 additions & 0 deletions packages/ajax/test/Ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ describe('Ajax', () => {
}
expect(thrown).to.be.true;
});

it('throws on 4xx responses, will allow parsing response manually', async () => {
ajax.addRequestInterceptor(async () => new Response('my response', { status: 400 }));

let thrown = false;
try {
await ajax.fetch('/foo');
} catch (e) {
// https://github.com/microsoft/TypeScript/issues/20024 open issue, can't type catch clause in param
const _e = /** @type {AjaxFetchError} */ (e);
expect(_e).to.be.an.instanceOf(AjaxFetchError);
expect(_e.request).to.be.an.instanceOf(Request);
expect(_e.response).to.be.an.instanceOf(Response);
const body = await _e.response.text();
expect(body).to.equal('my response');
thrown = true;
}
expect(thrown).to.be.true;
});
});

describe('fetchJson', () => {
Expand Down Expand Up @@ -183,6 +202,24 @@ describe('Ajax', () => {
expect(response.body).to.eql({ a: 1, b: 2 });
});

it('throws on 4xx responses, but still attempts parsing response body when using fetchJson', async () => {
ajax.addRequestInterceptor(async () => new Response('my response', { status: 400 }));

let thrown = false;
try {
await ajax.fetchJson('/foo');
} catch (e) {
// https://github.com/microsoft/TypeScript/issues/20024 open issue, can't type catch clause in param
const _e = /** @type {AjaxFetchError} */ (e);
expect(_e).to.be.an.instanceOf(AjaxFetchError);
expect(_e.request).to.be.an.instanceOf(Request);
expect(_e.response).to.be.an.instanceOf(Response);
expect(_e.body).to.equal('my response');
thrown = true;
}
expect(thrown).to.be.true;
});

describe('given a request body', () => {
it('encodes the request body as json', async () => {
await ajax.fetchJson('/foo', { method: 'POST', body: { a: 1, b: 2 } });
Expand Down

0 comments on commit 7a875ef

Please sign in to comment.