Skip to content

Commit

Permalink
fix(#3966): account for network errors (#3967)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Dec 19, 2024
1 parent c2933ef commit 84a9f85
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class RetryHandler {
}

onResponseError (controller, err) {
if (!controller || controller.aborted || isDisturbed(this.opts.body)) {
if (controller?.aborted || isDisturbed(this.opts.body)) {
this.handler.onResponseError?.(controller, err)
return
}
Expand Down
45 changes: 44 additions & 1 deletion test/interceptors/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { createServer } = require('node:http')
const { once } = require('node:events')

const { Client, interceptors } = require('../..')
const { retry, redirect } = interceptors
const { retry, redirect, dns } = interceptors

test('Should retry status code', async t => {
t = tspl(t, { plan: 4 })
Expand Down Expand Up @@ -74,6 +74,49 @@ test('Should retry status code', async t => {
t.equal(await response.body.text(), 'hello world!')
})

test('Should retry on error code', async t => {
t = tspl(t, { plan: 2 })

let counter = 0
const retryOptions = {
retry: (err, _state, done) => {
if (counter < 5) {
counter++
setTimeout(done, 500)
} else {
done(err)
}
},
maxRetries: 5
}
const requestOptions = {
origin: 'http://localhost:123',
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

const client = new Client(
'http://localhost:123'
).compose(dns({
lookup: (_h, _o, cb) => {
const error = new Error('ENOTFOUND')
error.code = 'ENOTFOUND'

cb(error)
}
}), retry(retryOptions))

after(async () => {
await client.close()
})

await t.rejects(client.request(requestOptions), { code: 'ENOTFOUND' })
t.equal(counter, 5)
})

test('Should use retry-after header for retries', async t => {
t = tspl(t, { plan: 3 })

Expand Down

0 comments on commit 84a9f85

Please sign in to comment.