node-telnet-client icon indicating copy to clipboard operation
node-telnet-client copied to clipboard

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

Open artemdudkin opened this issue 3 years ago • 1 comments

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)

artemdudkin avatar Dec 01 '22 10:12 artemdudkin

@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.

call8arshad avatar Feb 09 '23 11:02 call8arshad