Skip to content

Commit

Permalink
Expose HTTP errors that are not meant to be retried (nodejs#2496)
Browse files Browse the repository at this point in the history
* test: failing test to show http response not passed through

* fix: pass through requests that do not expect a retry

* refactor: cleanup
  • Loading branch information
MzUgM authored and crysmags committed Feb 27, 2024
1 parent cafa798 commit db95b63
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
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
)
})
})

0 comments on commit db95b63

Please sign in to comment.