Skip to content

Commit 26b9150

Browse files
committed
Fix reporting of abort errors in Node v20, where there may be multiple
1 parent f060e2e commit 26b9150

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

custom-typings/node-type-extensions.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,8 @@ declare module "http2" {
112112
__lastHopConnectAddress?: net.Socket['__lastHopConnectAddress'];
113113
__timingInfo?: net.Socket['__timingInfo'];
114114
}
115+
}
116+
117+
declare class AggregateError extends Error {
118+
errors: Error[]
115119
}

src/rules/requests/request-handlers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,13 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
10131013
// connection-level issue, so we try to create connection issues downstream.
10141014
resetOrDestroy(clientReq);
10151015

1016-
throw new AbortError(`Upstream connection error: ${
1017-
e.message ?? e
1018-
}`, e.code);
1016+
// Aggregate errors can be thrown if multiple (IPv4/6) addresses were tested. Note that
1017+
// AggregateError only exists in Node 15+. If that happens, we need to combine errors:
1018+
const errorMessage = typeof AggregateError !== 'undefined' && (e instanceof AggregateError)
1019+
? e.errors.map(e => e.message).join(', ')
1020+
: (e.message ?? e.code ?? e);
1021+
1022+
throw new AbortError(`Upstream connection error: ${errorMessage}`, e.code);
10191023
} else {
10201024
e.statusCode = 502;
10211025
e.statusMessage = 'Error communicating with upstream server';

test/integration/subscriptions/response-events.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ describe("Abort subscriptions", () => {
473473

474474
expect(seenAbort.error!.message).to.be.oneOf([
475475
'Upstream connection error: connect ECONNREFUSED 127.0.0.1:8999',
476-
'Upstream connection error: connect ECONNREFUSED ::1:8999'
476+
'Upstream connection error: connect ECONNREFUSED ::1:8999',
477+
'Upstream connection error: connect ECONNREFUSED ::1:8999, connect ECONNREFUSED 127.0.0.1:8999'
477478
]);
478479
expect(seenAbort.error!.code).to.equal('ECONNREFUSED');
479480
});

0 commit comments

Comments
 (0)