Skip to content

Commit

Permalink
catch all errors even if the package is used with nock for example
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrelaud committed Apr 12, 2023
1 parent 284085f commit 5f43788
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Rock-req

### 5.0.2
- Catch all errors even if the package is used with nock for example

### 5.0.1
- On relative redirect, `beforeRequest` handler receives updated `opts`

Expand Down
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ function extend (defaultOptions = {}) {
res.resume() // Discard response, consume data until the end to free up memory. Mandatory!
return setTimeout(rock, opts.retryDelay, opts, cb) // retry later
}

// or redirect and leave
if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
requestAbortedOrEnded = true // discard all new events which could come after for this request to avoid calling the callback
Expand All @@ -111,7 +110,6 @@ function extend (defaultOptions = {}) {
}
return rock(opts, cb)
}

// or read response and leave at the end
response = res
const contentEncoding = opts.method !== 'HEAD' ? (res.headers['content-encoding'] || '').toLowerCase() : ''
Expand All @@ -127,16 +125,18 @@ function extend (defaultOptions = {}) {
}
})
req.once('timeout', () => {
// This timeout event can come after the input pipeline is finished (ex. timeout with no body)
const _error = new Error('TimeoutError'); _error.code = 'ETIMEDOUT'
req.destroy() // we must destroy manually
onRequestEnd(_error) // This timeout event can come after the input pipeline is finished (ex. timeout with no body)
req.destroy(_error) // we must destroy manually and send the error to the error listener to call onRequestEnd
})
req.once('error', (e) => {
onRequestEnd(e) // error can happen before pipeline is executed when some interceptor are used such as nock
req.destroy()
})

const _inputStream = isFnStream(body) ? body(opts) : Readable.from([body], { objectMode: false })
pipeline(_inputStream, req, (e) => {
if (e) onRequestEnd(e)
})

return req
}

Expand All @@ -153,6 +153,5 @@ function extend (defaultOptions = {}) {
rock.concat = rock
rock.defaults = _default
rock.extend = extend

return rock
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rock-req",
"description": "Zero dependencies (160 LOC) & rock-solid request library: http/https, reliable retry on failure, redirects, gzip/deflate/brotli, extensible, proxy, streams, JSON mode, forms, timeout",
"version": "5.0.1",
"version": "5.0.2",
"author": {
"name": "David Grelaud & Feross Aboukhadijeh"
},
Expand Down
3 changes: 2 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ test('HEAD request', function (t) {
})

test('timeout option', function (t) {
t.plan(2)
t.plan(3)

const server = http.createServer(function (req, res) {
t.equal(req.url, '/path')
Expand All @@ -117,6 +117,7 @@ test('timeout option', function (t) {
maxRetry: 0
}, function (err, res) {
t.ok(err instanceof Error)
t.equal(err.message, 'TimeoutError')
server.close()
})
})
Expand Down

0 comments on commit 5f43788

Please sign in to comment.