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

Expose HTTP errors that are not meant to be retried #2496

Merged
merged 3 commits into from
Dec 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/handler/RetryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,22 @@ class RetryHandler {
this.retryCount += 1

if (statusCode >= 300) {
this.abort(
new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
})
)
return false
if (this.retryOpts.statusCodes.includes(statusCode) === false) {
return this.handler.onHeaders(
statusCode,
rawHeaders,
resume,
statusMessage
)
} else {
this.abort(
new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
})
)
return false
}
}

// Checkpoint for resume from where we left it
Expand Down
69 changes: 69 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,72 @@ tap.test('retrying a request with a body', t => {
)
})
})

tap.test('should not error if request is not meant to be retried', t => {
const server = createServer()
server.on('request', (req, res) => {
res.writeHead(400)
res.end('Bad request')
})

t.plan(3)

const dispatchOptions = {
retryOptions: {
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}
}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const chunks = []
const handler = new RetryHandler(dispatchOptions, {
dispatch: client.dispatch.bind(client),
handler: {
onConnect () {
t.pass()
},
onBodySent () {
t.pass()
},
onHeaders (status, _rawHeaders, resume, _statusMessage) {
t.equal(status, 400)
return true
},
onData (chunk) {
chunks.push(chunk)
return true
},
onComplete () {
t.equal(Buffer.concat(chunks).toString('utf-8'), 'Bad request')
},
onError (err) {
console.log({ err })
t.fail()
}
}
})

t.teardown(async () => {
await client.close()
server.close()

await once(server, 'close')
})

client.dispatch(
{
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
},
handler
)
})
})