sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 24 forks source link

fix: catch and re-reject failed promise from sftp() #72

Closed jeffrson closed 2 years ago

jeffrson commented 2 years ago

I had a hard time with occasional crashs of my app due to rejection in https://github.com/sanketbajoria/ssh2-promise/blob/master/src/sshConnection.ts#L80

It could happen, if the server closes the connection (for whatever reason) before sftp subsystem is "ready", maybe during completion of handshake. The error from ssh2 is "No response from server", originated in ssh2\lib\client.js:764:19.

Turns out, the rejected promise is not handled in https://github.com/sanketbajoria/ssh2-promise/blob/master/src/sftp.ts#L29 so the app crashs with "unhandled promise" exception.

My fix catches the rejected async promise and uses the error for rejecting the current promise.

Now, the error can be handled by the application:

sftp.readdir.then().catch()
sanketbajoria commented 2 years ago

thanks @jeffrson for fixing it up...

@jeffrson due to time constraints. i am looking out for maintainer. If you like to be maintainer of this project. Please let me know.