Skip to content

Commit

Permalink
fix: handle unhandled rejection when cannot connect to proxy (fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jun 21, 2019
1 parent 307cab6 commit 02086d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/classes/Agent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import EventEmitter from 'events';
import serializeError from 'serialize-error';
import Logger from '../Logger';
import type {
AgentType,
Expand Down Expand Up @@ -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);
}
});
Expand Down
10 changes: 10 additions & 0 deletions test/global-agent/routines/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

Expand Down

0 comments on commit 02086d9

Please sign in to comment.