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

Connection error causes end of program, i.e. does not go to connection.on('error') #225

Open
artemdudkin opened this issue Dec 1, 2022 · 1 comment

Comments

@artemdudkin
Copy link

artemdudkin commented Dec 1, 2022

If i start below script with arbitrary port, it causes unexpected end of program (i.e. there is no '3s' at console output)
(while promises and await is ok)

const { Telnet } = require('telnet-client')

const stime = Date.now();

const connection = new Telnet()

const params = {
    host: '127.0.0.1',
    port: 56565,
    shellPrompt: '>INFO',
    timeout: 1000,
    debug: true
}

connection.on('ready', prompt => connection.send('status'))

connection.on('data', data => {
    console.log('>> ', data.toString());
    if (data.indexOf('\nEND') !== -1) connection.end()
})

connection.on('timeout', () => {
    console.log('timeout')
    connection.end()
})

connection.on('close', () => console.log('connection closed'))

connection.on('error', err => {
  console.log('ERROR', err)
})

connection.connect(params)




setTimeout(()=>{console.log('3s')}, 3000)
@call8arshad
Copy link

@artemdudkin it's going to connection.on('error')
Screen Shot 2023-02-09 at 4 51 59 PM

If you want to avoid the end of the program.
that you can achieve in 3 ways.

  1. use promise
connection.connect(params).then().catch(err => {
    console.log('Do something for this connection error')
})
  1. use await
try {
  await connection.connect(params)
} catch (err) {
  console.log('Do something for this connection error');
}
  1. handle unhandledRejection events
process.on('unhandledRejection', error => {
  console.log('Do something about it: ', error)
  // throw error
})

With all the above 3, you can see '3s' in the console along with the error message.
It's up to the user what he wants to do with the error.

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

No branches or pull requests

2 participants