Skip to content

Commit

Permalink
fix: log errors on retry
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Apr 30, 2024
1 parent 3689e85 commit 4d82234
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Minipass } = require('minipass')
const fetch = require('minipass-fetch')
const promiseRetry = require('promise-retry')
const ssri = require('ssri')
const { log } = require('proc-log')

const CachingMinipassPipeline = require('./pipeline.js')
const { getAgent } = require('@npmcli/agent')
Expand Down Expand Up @@ -89,6 +90,7 @@ const remoteFetch = (request, options) => {
options.onRetry(res)
}

log.http('fetch', `${req.method} ${req.url} failed with ${res.status}, retrying`)
return retryHandler(res)
}

Expand All @@ -112,6 +114,7 @@ const remoteFetch = (request, options) => {
options.onRetry(err)
}

log.http('fetch', `${req.method} ${req.url} failed with ${err.code}, retrying`)
return retryHandler(err)
}
}, options.retry).catch((err) => {
Expand Down
26 changes: 26 additions & 0 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ t.test('500-level responses not thrown', async t => {

t.test('calls opts.onRetry', async t => {
t.test('when request is retriable', async (t) => {
const logs = []
const logHandler = (...logparams) => {
logs.push(...logparams)
}
process.on('log', logHandler)
t.teardown(() => {
process.off('log', logHandler)
})
const srv = nock(HOST)
.get('/test-onretry')
.reply(500)
Expand All @@ -95,10 +103,23 @@ t.test('calls opts.onRetry', async t => {
t.same(res.headers.get('x-fetch-attempts'), '2', 'set the appropriate header')
t.equal(calledOnRetry, true, 'should have called onRetry')
t.equal(retryNotification, 1, 'should have called method once')
t.strictSame(logs, [
'http',
'fetch',
'GET https://make-fetch-happen.npm/test-onretry failed with 500, retrying',
])
t.ok(srv.isDone())
})

t.test('when request is retriable; and caught', async (t) => {
const logs = []
const logHandler = (...logparams) => {
logs.push(...logparams)
}
process.on('log', logHandler)
t.teardown(() => {
process.off('log', logHandler)
})
const srv = nock(HOST)
.get('/catch-retry')
.replyWithError({
Expand Down Expand Up @@ -128,6 +149,11 @@ t.test('calls opts.onRetry', async t => {
t.equal(calledOnRetry, true, 'should have called onRetry')
const buf = await res.buffer()
t.same(buf, CONTENT, 'request succeeded')
t.strictSame(logs, [
'http',
'fetch',
'GET https://make-fetch-happen.npm/catch-retry failed with ECONNRESET, retrying',
])
t.ok(srv.isDone())
})

Expand Down

0 comments on commit 4d82234

Please sign in to comment.