Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Catch error when connection lost #25

@lmdc45

Description

@lmdc45

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions