diff --git a/packages/headless/src/api/platform-client.test.ts b/packages/headless/src/api/platform-client.test.ts index a35ecf41d93..f3c7ffa8d4a 100644 --- a/packages/headless/src/api/platform-client.test.ts +++ b/packages/headless/src/api/platform-client.test.ts @@ -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 diff --git a/packages/headless/src/api/platform-client.ts b/packages/headless/src/api/platform-client.ts index 801120b087e..45e759d4c68 100644 --- a/packages/headless/src/api/platform-client.ts +++ b/packages/headless/src/api/platform-client.ts @@ -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);