The SftpClient wraps the event-based ssh2 API into a Promise-based API.
Promise-based interactions
During an active API call, the client attaches temporary event listeners (error, end, close) to the underlying socket. If an event occurs while a promise is pending, the promise is rejected to communicate the failure to your try/catch or .catch() block.
Global event handlers
If an event (like a lost connection) occurs outside the context of an active API call, no promise exists to catch it. In these cases, the client uses global event handlers which, by default, log the event and invalidate the current connection.
To handle these 'out-of-band' events, you can provide custom callbacks when instantiating the SftpClient constructor:
// Example of providing global event listeners
let sftp = new Client({
error: (err) => console.error('Global error:', err),
end: () => console.log('Connection ended'),
close: () => console.log('Connection closed')
});
Note: The error callback receives the error object as an argument; end and close callbacks receive no arguments.