Skip to content

Commit 84a9f85

Browse files
authored
fix(nodejs#3966): account for network errors (nodejs#3967)
1 parent c2933ef commit 84a9f85

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/handler/retry-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class RetryHandler {
277277
}
278278

279279
onResponseError (controller, err) {
280-
if (!controller || controller.aborted || isDisturbed(this.opts.body)) {
280+
if (controller?.aborted || isDisturbed(this.opts.body)) {
281281
this.handler.onResponseError?.(controller, err)
282282
return
283283
}

test/interceptors/retry.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { createServer } = require('node:http')
66
const { once } = require('node:events')
77

88
const { Client, interceptors } = require('../..')
9-
const { retry, redirect } = interceptors
9+
const { retry, redirect, dns } = interceptors
1010

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

77+
test('Should retry on error code', async t => {
78+
t = tspl(t, { plan: 2 })
79+
80+
let counter = 0
81+
const retryOptions = {
82+
retry: (err, _state, done) => {
83+
if (counter < 5) {
84+
counter++
85+
setTimeout(done, 500)
86+
} else {
87+
done(err)
88+
}
89+
},
90+
maxRetries: 5
91+
}
92+
const requestOptions = {
93+
origin: 'http://localhost:123',
94+
method: 'GET',
95+
path: '/',
96+
headers: {
97+
'content-type': 'application/json'
98+
}
99+
}
100+
101+
const client = new Client(
102+
'http://localhost:123'
103+
).compose(dns({
104+
lookup: (_h, _o, cb) => {
105+
const error = new Error('ENOTFOUND')
106+
error.code = 'ENOTFOUND'
107+
108+
cb(error)
109+
}
110+
}), retry(retryOptions))
111+
112+
after(async () => {
113+
await client.close()
114+
})
115+
116+
await t.rejects(client.request(requestOptions), { code: 'ENOTFOUND' })
117+
t.equal(counter, 5)
118+
})
119+
77120
test('Should use retry-after header for retries', async t => {
78121
t = tspl(t, { plan: 3 })
79122

0 commit comments

Comments
 (0)