From 8001e0ac3f6fe65b2dfe4f906d81412147dd63b7 Mon Sep 17 00:00:00 2001
From: Paul Marrapese <paul.marrapese@gmail.com>
Date: Sun, 5 Apr 2020 18:21:15 -0700
Subject: [PATCH] improved exception handling for Exec

---
 lib/utils/index.js | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/utils/index.js b/lib/utils/index.js
index 981047b6..ab1b4eb4 100644
--- a/lib/utils/index.js
+++ b/lib/utils/index.js
@@ -156,17 +156,19 @@ utils.mixin(Exec.prototype, new (function () {
     let args;
     let next = this._cmds.shift();
     let config = this._config;
-    let errData = '';
+    let errObj;
     let shStdio;
     let handleStdoutData = function (data) {
       self.emit('stdout', data);
     };
     let handleStderrData = function (data) {
-      let d = data.toString();
       self.emit('stderr', data);
+
       // Accumulate the error-data so we can use it as the
       // stack if the process exits with an error
-      errData += d;
+      if (!errObj) errObj = new Error();
+      let d = data.toString();
+      if (d) errObj.message += d;
     };
 
     // Keep running as long as there are commands in the array
@@ -219,11 +221,10 @@ utils.mixin(Exec.prototype, new (function () {
 
       // Exit, handle err or run next
       sh.on('exit', function (code) {
-        let msg;
         if (code !== 0) {
-          msg = errData || 'Process exited with error.';
-          msg = utils.string.trim(msg);
-          self.emit('error', msg, code);
+          if (!errObj.message) errObj.message = 'Process exited with error.';
+          errObj.message = utils.string.trim(errObj.message);
+          self.emit('error', errObj, code);
         }
         if (code === 0 || !config.breakOnError) {
           self.emit('cmdEnd', next);