TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
871 stars 229 forks source link

Not caught Error: Client network socket disconnected before secure TLS connection was established #277

Open AidanWelch opened 4 months ago

AidanWelch commented 4 months ago

It's normal that some proxied requests fail, so I have simple error handling, the issue is that the error is not being caught and instead crashing the script,

import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';

const agent = new HttpsProxyAgent( process.env.PROXY_STRING );

let err;
const casePageResponse = await fetch( `foo.bar`, { agent })
    .catch( e => err = e );

Crashes with this error:

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: Client network socket disconnected before secure TLS connection was established
    at TLSSocket.onConnectEnd (node:_tls_wrap:1727:19)
    at TLSSocket.emit (node:events:531:35)
    at endReadableNT (node:internal/streams/readable:1696:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ECONNRESET',
  path: undefined,
  host: 'us-pr.oxylabs.io',
  port: 10000,
  localAddress: undefined
}

It doesn't get caught in the catch statement.

AidanWelch commented 4 months ago

Probably an error with node-fetch as seen on this tangentially related issue: https://github.com/node-fetch/node-fetch/issues/892

AidanWelch commented 4 months ago

Actually I get it with node:https too, also not being caught by req.on('error', console.error)

AidanWelch commented 4 months ago

passing rejectUnauthorized like so:

const agent = new HttpsProxyAgent( process.env.PROXY_STRING, { rejectUnauthorized: false });

stops this error specifically, but the issue is still that it doesn't throw the error.

florrdv commented 2 months ago

Having the same issue here!