From d5173a31c559d3526a642173d48c7b18838a2705 Mon Sep 17 00:00:00 2001 From: 7ttp <117663341+7ttp@users.noreply.github.com> Date: Sat, 1 Aug 2026 00:11:40 +0530 Subject: [PATCH 1/3] fix(auth): preserve 5xx error message --- packages/core/auth-js/src/lib/fetch.ts | 13 +++--- packages/core/auth-js/test/fetch.test.ts | 55 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/packages/core/auth-js/src/lib/fetch.ts b/packages/core/auth-js/src/lib/fetch.ts index cbf73097711..8e3dc62260d 100644 --- a/packages/core/auth-js/src/lib/fetch.ts +++ b/packages/core/auth-js/src/lib/fetch.ts @@ -81,18 +81,21 @@ export async function handleError(error: unknown) { throw new AuthRetryableFetchError(_getErrorMessage(error), 0) } - if (NETWORK_ERROR_CODES.includes(error.status)) { - // status in 500...599 range - server had an error, request might be retryed. - throw new AuthRetryableFetchError(_getErrorMessage(error), error.status) - } - let data: any try { data = await error.json() } catch (e) { + if (NETWORK_ERROR_CODES.includes(error.status)) { + throw new AuthRetryableFetchError(_getErrorMessage(error), error.status) + } throw new AuthUnknownError(_getErrorMessage(e), e) } + if (NETWORK_ERROR_CODES.includes(error.status)) { + // status in 500...599 range - server had an error, request might be retryed. + throw new AuthRetryableFetchError(_getErrorMessage(data), error.status) + } + let errorCode: string | undefined = undefined const responseAPIVersion = parseResponseAPIVersion(error) diff --git a/packages/core/auth-js/test/fetch.test.ts b/packages/core/auth-js/test/fetch.test.ts index 7b099ebbf1a..a937da1c56b 100644 --- a/packages/core/auth-js/test/fetch.test.ts +++ b/packages/core/auth-js/test/fetch.test.ts @@ -65,6 +65,32 @@ describe('fetch', () => { expect(route).toHaveBeenCalledTimes(1) }) + test('should preserve the server-sent JSON message on a retryable 500 error', async () => { + const route = server + .get('/') + .mockImplementationOnce((ctx) => { + ctx.status = 500 + ctx.body = { + code: 500, + error_code: 'unexpected_failure', + msg: 'Error sending confirmation email', + } + }) + .mockImplementation((ctx) => { + ctx.status = 200 + }) + + const url = server.getURL().toString() + + const error = await _request(fetch, 'GET', url).catch((e) => e) + + expect(error).toBeInstanceOf(AuthRetryableFetchError) + expect(error.status).toEqual(500) + expect(error.message).toEqual('Error sending confirmation email') + + expect(route).toHaveBeenCalledTimes(1) + }) + test('should throw AuthRetryableFetchError upon Cloudflare edge errors (525)', async () => { const route = server .get('/') @@ -257,6 +283,35 @@ describe('handleError', () => { expect(error.code).toEqual(example.code) }) }) + + it('preserves the server-sent message for a retryable 5xx with a JSON body', async () => { + const response = new Response( + JSON.stringify({ + code: 500, + error_code: 'unexpected_failure', + msg: 'Error sending confirmation email', + }), + { status: 500, statusText: 'Internal Server Error' } + ) + + const error: any = await handleError(response).catch((e) => e) + + expect(error).toBeInstanceOf(AuthRetryableFetchError) + expect(error.status).toEqual(500) + expect(error.message).toEqual('Error sending confirmation email') + }) + + it('stays retryable for a 5xx without a JSON body', async () => { + const response = new Response('

502 Bad Gateway

', { + status: 502, + statusText: 'Bad Gateway', + }) + + const error: any = await handleError(response).catch((e) => e) + + expect(error).toBeInstanceOf(AuthRetryableFetchError) + expect(error.status).toEqual(502) + }) }) describe('_sessionResponse', () => { From 45e4e2aa38d34423961d9e3adbe42a7a7c75de32 Mon Sep 17 00:00:00 2001 From: 7ttp <117663341+7ttp@users.noreply.github.com> Date: Mon, 3 Aug 2026 13:15:08 +0530 Subject: [PATCH 2/3] Nit --- packages/core/auth-js/src/lib/fetch.ts | 3 +- packages/core/auth-js/test/fetch.test.ts | 45 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/packages/core/auth-js/src/lib/fetch.ts b/packages/core/auth-js/src/lib/fetch.ts index 8e3dc62260d..bd2e6844627 100644 --- a/packages/core/auth-js/src/lib/fetch.ts +++ b/packages/core/auth-js/src/lib/fetch.ts @@ -86,7 +86,8 @@ export async function handleError(error: unknown) { data = await error.json() } catch (e) { if (NETWORK_ERROR_CODES.includes(error.status)) { - throw new AuthRetryableFetchError(_getErrorMessage(error), error.status) + // statusText can be empty — HTTP/2 has no reason phrase + throw new AuthRetryableFetchError(error.statusText || `HTTP ${error.status}`, error.status) } throw new AuthUnknownError(_getErrorMessage(e), e) } diff --git a/packages/core/auth-js/test/fetch.test.ts b/packages/core/auth-js/test/fetch.test.ts index a937da1c56b..97ae93a1ca5 100644 --- a/packages/core/auth-js/test/fetch.test.ts +++ b/packages/core/auth-js/test/fetch.test.ts @@ -70,10 +70,10 @@ describe('fetch', () => { .get('/') .mockImplementationOnce((ctx) => { ctx.status = 500 + ctx.set(API_VERSION_HEADER_NAME, '2024-01-01') ctx.body = { - code: 500, - error_code: 'unexpected_failure', - msg: 'Error sending confirmation email', + code: 'unexpected_failure', + message: 'Error sending confirmation email', } }) .mockImplementation((ctx) => { @@ -284,7 +284,29 @@ describe('handleError', () => { }) }) - it('preserves the server-sent message for a retryable 5xx with a JSON body', async () => { + it('preserves the server-sent message for a retryable 5xx with API version 2024-01-01', async () => { + const response = new Response( + JSON.stringify({ + code: 'unexpected_failure', + message: 'Error sending confirmation email', + }), + { + status: 500, + statusText: 'Internal Server Error', + headers: { + [API_VERSION_HEADER_NAME]: '2024-01-01', + }, + } + ) + + const error: any = await handleError(response).catch((e) => e) + + expect(error).toBeInstanceOf(AuthRetryableFetchError) + expect(error.status).toEqual(500) + expect(error.message).toEqual('Error sending confirmation email') + }) + + it('preserves the server-sent message for a retryable 5xx without API version', async () => { const response = new Response( JSON.stringify({ code: 500, @@ -301,7 +323,7 @@ describe('handleError', () => { expect(error.message).toEqual('Error sending confirmation email') }) - it('stays retryable for a 5xx without a JSON body', async () => { + it('falls back to the status text for a 5xx without a JSON body', async () => { const response = new Response('

502 Bad Gateway

', { status: 502, statusText: 'Bad Gateway', @@ -311,6 +333,19 @@ describe('handleError', () => { expect(error).toBeInstanceOf(AuthRetryableFetchError) expect(error.status).toEqual(502) + expect(error.message).toEqual('Bad Gateway') + }) + + it('falls back to the status code when there is no status text (HTTP/2)', async () => { + const response = new Response('

502 Bad Gateway

', { + status: 502, + }) + + const error: any = await handleError(response).catch((e) => e) + + expect(error).toBeInstanceOf(AuthRetryableFetchError) + expect(error.status).toEqual(502) + expect(error.message).toEqual('HTTP 502') }) }) From 0986666836293f6d4884fce6000dfcd022c1a00d Mon Sep 17 00:00:00 2001 From: 7ttp <117663341+7ttp@users.noreply.github.com> Date: Mon, 3 Aug 2026 15:32:42 +0530 Subject: [PATCH 3/3] rerun