From 02086d96665d2167f6dcb357d76ebaef7337fec4 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Fri, 21 Jun 2019 09:38:05 +0200 Subject: [PATCH] fix: handle unhandled rejection when cannot connect to proxy (fixes #1) --- src/classes/Agent.js | 7 +++++++ test/global-agent/routines/bootstrap.js | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/classes/Agent.js b/src/classes/Agent.js index 5f7659c5..04c661ca 100644 --- a/src/classes/Agent.js +++ b/src/classes/Agent.js @@ -1,6 +1,7 @@ // @flow import EventEmitter from 'events'; +import serializeError from 'serialize-error'; import Logger from '../Logger'; import type { AgentType, @@ -111,6 +112,12 @@ class Agent { if (error) { request.emit('error', error); } else { + socket.on('error', (socketError) => { + log.error({ + error: serializeError(socketError) + }, 'socket error'); + }); + request.onSocket(socket); } }); diff --git a/test/global-agent/routines/bootstrap.js b/test/global-agent/routines/bootstrap.js index a7b31b87..c318d9b1 100644 --- a/test/global-agent/routines/bootstrap.js +++ b/test/global-agent/routines/bootstrap.js @@ -178,6 +178,16 @@ test('proxies HTTPS request', async (t) => { t.assert(response.body === 'OK'); }); +test('does not produce unhandled rejection when cannot connect to proxy', async (t) => { + bootstrap({}); + + const port = await getNextPort(); + + global.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:' + port; + + await t.throwsAsync(got('http://127.0.0.1')); +}); + test('proxies HTTPS request with dedicated proxy', async (t) => { bootstrap({});