diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 28006353a35f34..c936bc16228d33 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -284,9 +284,8 @@ function handleSocketAfterProxy(err, req) { if (err.code === 'ERR_PROXY_TUNNEL') { if (err.proxyTunnelTimeout) { req.emit('timeout'); // Propagate the timeout from the tunnel to the request. - } else { - req.emit('error', err); } + req.emit('error', err); } } diff --git a/lib/_http_client.js b/lib/_http_client.js index 63640301d6dbdb..ee4f47be64ab3c 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -958,7 +958,8 @@ function onSocketNT(req, socket, err) { if (!req.aborted && !err) { err = new ConnResetException('socket hang up'); } - if (err) { + // ERR_PROXY_TUNNEL is handled by the proxying logic + if (err && err.code !== 'ERR_PROXY_TUNNEL') { emitErrorEvent(req, err); } req._closed = true; diff --git a/test/client-proxy/test-https-proxy-request-malformed-response.mjs b/test/client-proxy/test-https-proxy-request-malformed-response.mjs index edaeeda13353e7..0f096d9bec8d9f 100644 --- a/test/client-proxy/test-https-proxy-request-malformed-response.mjs +++ b/test/client-proxy/test-https-proxy-request-malformed-response.mjs @@ -43,7 +43,7 @@ const { code, signal, stderr, stdout } = await runProxiedRequest({ }); // The proxy client should get an error from failure in establishing the tunnel. -assert.match(stderr, /ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* NOT-HTTP MALFORMED RESPONSE/); +assert.strictEqual(stderr.match(/ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* NOT-HTTP MALFORMED RESPONSE*/g).length, 1); assert.strictEqual(stdout.trim(), ''); assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/client-proxy/test-https-proxy-request-proxy-failure-404.mjs b/test/client-proxy/test-https-proxy-request-proxy-failure-404.mjs index df1dfd8e3b79cd..7b61d81301a3b7 100644 --- a/test/client-proxy/test-https-proxy-request-proxy-failure-404.mjs +++ b/test/client-proxy/test-https-proxy-request-proxy-failure-404.mjs @@ -43,7 +43,7 @@ const { code, signal, stderr, stdout } = await runProxiedRequest({ }); // The proxy client should get an error from failure in establishing the tunnel. -assert.match(stderr, /ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 404 Not Found/); +assert.strictEqual(stderr.match(/ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 404 Not Found/g).length, 1); assert.strictEqual(stdout.trim(), ''); assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/client-proxy/test-https-proxy-request-proxy-failure-500.mjs b/test/client-proxy/test-https-proxy-request-proxy-failure-500.mjs index cb5f040533518f..e57c1bb16cb071 100644 --- a/test/client-proxy/test-https-proxy-request-proxy-failure-500.mjs +++ b/test/client-proxy/test-https-proxy-request-proxy-failure-500.mjs @@ -44,7 +44,7 @@ const { code, signal, stderr, stdout } = await runProxiedRequest({ }); // The proxy client should get an error from failure in establishing the tunnel. -assert.match(stderr, /ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 500 Connection Error/); +assert.strictEqual(stderr.match(/ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 500 Connection Error/g).length, 1); assert.strictEqual(stdout.trim(), ''); assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/client-proxy/test-https-proxy-request-proxy-failure-502.mjs b/test/client-proxy/test-https-proxy-request-proxy-failure-502.mjs index 4d7f42508877b3..1241c38f593738 100644 --- a/test/client-proxy/test-https-proxy-request-proxy-failure-502.mjs +++ b/test/client-proxy/test-https-proxy-request-proxy-failure-502.mjs @@ -42,7 +42,7 @@ const { code, signal, stderr, stdout } = await runProxiedRequest({ }); // The proxy client should get an error from failure in establishing the tunnel. -assert.match(stderr, /ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 502 Bad Gateway/); +assert.strictEqual(stderr.match(/ERR_PROXY_TUNNEL.*Failed to establish tunnel to .* HTTP\/1\.1 502 Bad Gateway/g).length, 1); assert.strictEqual(stdout.trim(), ''); assert.strictEqual(code, 0); assert.strictEqual(signal, null);