Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/core/auth-js/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ 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)) {
// 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)
}

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)
Expand Down
90 changes: 90 additions & 0 deletions packages/core/auth-js/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.set(API_VERSION_HEADER_NAME, '2024-01-01')
ctx.body = {
code: 'unexpected_failure',
message: '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('/')
Expand Down Expand Up @@ -257,6 +283,70 @@ describe('handleError', () => {
expect(error.code).toEqual(example.code)
})
})

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,
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('falls back to the status text for a 5xx without a JSON body', async () => {
const response = new Response('<html><body><h1>502 Bad Gateway</h1></body></html>', {
status: 502,
statusText: 'Bad Gateway',
})

const error: any = await handleError(response).catch((e) => e)

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('<html><body><h1>502 Bad Gateway</h1></body></html>', {
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')
})
})

describe('_sessionResponse', () => {
Expand Down
Loading