From d4bebd066a9a4b5ae8225f6609c6d2ed1a932880 Mon Sep 17 00:00:00 2001 From: gaberudy Date: Sun, 29 Sep 2024 07:25:30 -0600 Subject: [PATCH] Fix handling of SSH network errors by using error callback instead of throwing an exception when appropriate --- lib/ssh.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ssh.js b/lib/ssh.js index 2d44564..c4a14f7 100644 --- a/lib/ssh.js +++ b/lib/ssh.js @@ -10,13 +10,13 @@ module.exports = function (opt) { conn.once('ready', function () { conn.exec('docker system dial-stdio', function (err, stream) { if (err) { - handleError(err); + handleError(err , fn); } fn(null, stream); stream.addListener('error', (err) => { - handleError(err); + handleError(err, fn); }); stream.once('close', () => { conn.end(); @@ -24,7 +24,7 @@ module.exports = function (opt) { }); }); }).on('error', (err) => { - handleError(err); + handleError(err, fn); }) .connect(opt); conn.once('end', () => agent.destroy()); @@ -34,10 +34,14 @@ module.exports = function (opt) { } }; - function handleError(err) { + function handleError(err, cb) { conn.end(); agent.destroy(); - throw err; + if (cb) { + cb(err); + } else { + throw err; + } } return agent;