Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Catch error when connection lost #25

Open
lmdc45 opened this issue Jul 20, 2020 · 1 comment
Open

Catch error when connection lost #25

lmdc45 opened this issue Jul 20, 2020 · 1 comment

Comments

@lmdc45
Copy link

lmdc45 commented Jul 20, 2020

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?

@michaelfranklin01
Copy link

I need an answer to this as well.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants