Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Question : How to handle errors #116

Closed
is984 opened this issue Sep 13, 2023 · 1 comment · Fixed by #118
Closed

Question : How to handle errors #116

is984 opened this issue Sep 13, 2023 · 1 comment · Fixed by #118

Comments

@is984
Copy link
Contributor

is984 commented Sep 13, 2023

I implemented the following.

async function openSSHTunnel(config){
    const tOption = {
        autoClose: true
    }

    const sshOption = {
        host: config.ssh.host,
        port: config.ssh.port,
        username: config.ssh.username,
        password: config.ssh.password
    }
    const fOption = {
        dstAddr: config.ssh.dstHost,
        dstPort: config.ssh.dstPort
    }
    try {
        let res =await createTunnel(tOption, null, sshOption, fOption);
        return res
    } catch (e) {
        //Note: logger.write is a custom function
        logger.write(1, "Caught Error")
        logger.write(1, e)
        return Error(e)
    }
}

Passing incorrect SSH credentials to this code results in the following output and application termination.

Error : Caught Error
Error : Error: All configured authentication methods failed

C:\APPDir\node_modules\tunnel-ssh\index.js:94
            server.on('close', () => sshConnection.end());
                                                   ^

TypeError: Cannot read property 'end' of undefined
    at Server.<anonymous> (C:\APPDir\node_modules\tunnel-ssh\index.js:94:52)
    at Server.emit (events.js:314:20)
    at emitCloseNT (net.js:1658:8)
    at processTicksAndRejections (internal/process/task_queues.js:79:21)

The following can be gleaned from the above output

  1. the authentication failure error is handled correctly
  2. different error occurred after the error handle, so it could not be handled.

What should I do to resolve this situation (application terminated)?

@is984
Copy link
Contributor Author

is984 commented Nov 17, 2023

discovered that the cause was the order of function calls.
The addListServer is called first, which sets the close event on the server.
This defines the process of closing the sshConnection when the server terminates.
*See below.

   function addListenerServer(server) {
            if (tunnelOptionsLocal.reconnectOnError) {
                server.on('error', async () => {
                    server = await createServer(serverOptions);
                    addListenerServer(server);
                });
            }
            server.on('connection', onConnectionHandler);
            server.on('close', () => sshConnection.end()); //The line that causes
   }

After that, addListerSshConnection is called.
However, if the ssh connection fails here, the SSH connection failure is first returned as a reject.
Then the Server's Close event is executed and an error occurs as it tries to close the non-existent sshConnection.
However, since it has already returned a Reject, it is not properly handled and the application seems to crash.

Therefore, I believe that creating the SSH tunnel first and then the ServerListener will solve this problem.

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

Successfully merging a pull request may close this issue.

1 participant