Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved exception handling when a child process fails #373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down