Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide statusText as error message on retryable network errors #832

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function handleError(error: unknown) {

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)
throw new AuthRetryableFetchError(error.statusText, error.status)
}

let data: any
Expand Down
20 changes: 20 additions & 0 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ describe('fetch', () => {
await server.start()
})

test('should throw AuthRetryableFetchError when response is a network error', async () => {
const route = server
.get('/')
.mockImplementationOnce((ctx) => {
ctx.status = 504
ctx.statusText = 'Gateway Timeout'
ctx.body = 'Gateway Timeout'
})
.mockImplementation((ctx) => {
ctx.status = 200
})

const url = server.getURL().toString()
const result = _request(fetch, 'GET', url)
await expect(result).rejects.toBeInstanceOf(AuthRetryableFetchError)
await expect(result).rejects.toMatchObject({ status: 504, message: 'Gateway Timeout' })

expect(route).toHaveBeenCalledTimes(1)
})

test('should work with custom fetch implementation', async () => {
const customFetch = (async () => {
return {
Expand Down