diff --git a/lib/interceptor/dns.js b/lib/interceptor/dns.js index 98ef376ecbb..c8d56c2cf77 100644 --- a/lib/interceptor/dns.js +++ b/lib/interceptor/dns.js @@ -353,7 +353,7 @@ module.exports = interceptorOpts => { instance.runLookup(origin, origDispatchOpts, (err, newOrigin) => { if (err) { - return handler.onError(err) + return handler.onResponseError(null, err) } let dispatchOpts = null diff --git a/test/interceptors/dns.js b/test/interceptors/dns.js index 050d31ee2ec..6dc0420527c 100644 --- a/test/interceptors/dns.js +++ b/test/interceptors/dns.js @@ -1865,3 +1865,28 @@ test('#3937 - Handle host correctly', async t => { t.equal(response2.statusCode, 200) t.equal(await response2.body.text(), 'hello world!') }) + +test('#3951 - Should handle lookup errors correctly', async t => { + const suite = tspl(t, { plan: 1 }) + + const requestOptions = { + method: 'GET', + path: '/', + headers: { + 'content-type': 'application/json' + } + } + + const client = new Agent().compose([ + dns({ + lookup: (_origin, _opts, cb) => { + cb(new Error('lookup error')) + } + }) + ]) + + suite.rejects(client.request({ + ...requestOptions, + origin: 'http://localhost' + }), new Error('lookup error')) +})