Fail to understand how I should handle connection failures from a client.
I am trying to construct a client that will handle failure conditions correctly. I am currently testing a situation where the client tries to connect to a server that is not running. I can see at the socket level a tls.connect is failing with "Connection Refused", but this doesn't seem to be translating into anything I can use.
Here is a promise I am trying to create for the session
this.sessionPromise = new Promise((resolve,reject) => { function onSessionError(err) { session.removeListener('error', onSessionError); reject(err); } const session = http2.connect( `https://localhost:${process.env.PAS_HTTPS_PORT}`, { rejectUnauthorized: false}, () => { session.removeListener('error', onSessionError); resolve(session); } ); session.on('error', onSessionError); });
but that promise never resolves.
In a previous attempt, I didn't wait for the connection to establish, and after creating the connection immediately created a request - something like this
const session = http2.connect( `https://localhost:${process.env.PAS_HTTPS_PORT}`, { rejectUnauthorized: false} ); const req = session.request(headers) req.on('error', /*did something here */) req.on('end', /* did something here */)
In that scenario, the 'end' event was raised pretty quickly - but no error.event. Unfortunately that makes it very difficult to understand why the 'end' event was raised, so I didn't pursue it.
What is the correct way of dealing with this? docs don't give much clue.