Skip to content

Commit

Permalink
feat(headless): handle 401 like 419 (#4103)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart authored Jun 19, 2024
1 parent cfcc9d9 commit 6623e03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
17 changes: 10 additions & 7 deletions packages/headless/src/api/platform-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,17 @@ describe('PlatformClient call', () => {
);
});

it('when status is 419 should return a TokenExpiredError', async () => {
mockFetch.mockReturnValueOnce(
Promise.resolve(new Response(JSON.stringify({}), {status: 419}))
);
it.each([401, 419])(
'when status is %d should return a TokenExpiredError',
async (status) => {
mockFetch.mockReturnValueOnce(
Promise.resolve(new Response(JSON.stringify({}), {status}))
);

const response = await platformCall();
expect(response).toBeInstanceOf(ExpiredTokenError);
});
const response = await platformCall();
expect(response).toBeInstanceOf(ExpiredTokenError);
}
);

it('when status is 429 should try exponential backOff', async () => {
mockFetch
Expand Down
22 changes: 10 additions & 12 deletions packages/headless/src/api/platform-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,17 @@ export class PlatformClient {
return shouldRetry;
},
});

if (response.status === 419) {
logger.info('Platform renewing token');
throw new ExpiredTokenError();
}

if (response.status === 404) {
throw new DisconnectedError(url, response.status);
switch (response.status) {
case 419:
case 401:
logger.info('Platform renewing token');
throw new ExpiredTokenError();
case 404:
throw new DisconnectedError(url, response.status);
default:
logger.info({response, requestInfo}, 'Platform response');
return response;
}

logger.info({response, requestInfo}, 'Platform response');

return response;
} catch (error) {
if ((error as PlatformClientCallError).message === 'Failed to fetch') {
return new DisconnectedError(url);
Expand Down

0 comments on commit 6623e03

Please sign in to comment.