node-telnet-client
node-telnet-client copied to clipboard
Connection error causes end of program, i.e. does not go to connection.on('error')
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 it's going to connection.on('error')

If you want to avoid the end of the program. that you can achieve in 3 ways.
- use promise
connection.connect(params).then().catch(err => {
console.log('Do something for this connection error')
})
- use await
try {
await connection.connect(params)
} catch (err) {
console.log('Do something for this connection error');
}
- 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.