Stocard/node-ssh-forward

Catch error when connection lost

Opened this issue · 1 comments

I try to catch the error of connection to automatically try to reconnect if something happen on the network.

Once connected and if I disconnect from the network, an error is thrown like this after a few seconds:

node_modules\ssh2\lib\client.js:999
    throw new Error('Not connected');
    ^

Error: Not connected
    at Client.forwardOut (node_modules\ssh2\lib\client.js:999:11)
    at Server.<anonymous> (node_modules\node-ssh-forward\dist\Connection.js:185:28)
    at Server.emit (events.js:315:20)
    at TCP.onconnection (net.js:1555:8)

However it is not detected by the try,catch block around the connection.

export async function main() {
    try {
        let sshConnection = new SSHConnection(options);
        let res = await sshConnection.forward({
          fromPort: fp,
          toPort: tp,
        });
        console.log('connected',res);
    } catch(err){
        console.log('got an error, trying to restart connection',err);
        main();
    }
}

How could I detect this properly without stopping the process?

I need an answer to this as well.