Skip to content

Commit

Permalink
fix(core/exec): use nodejs style error messages when throwing (#16961)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Ladzaretti authored Aug 4, 2022
1 parent ff650ce commit 257d9a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/util/exec/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('util/exec/common', () => {
exec(cmd, partial<RawExecOptions>({ encoding: 'utf8' }))
).rejects.toMatchObject({
cmd,
message: 'Process exited with exit code "1"',
message: `Command failed: ${cmd}\n${stderr}`,
exitCode,
stderr,
});
Expand All @@ -200,7 +200,7 @@ describe('util/exec/common', () => {
).rejects.toMatchObject({
cmd,
signal: exitSignal,
message: 'Process signaled with "SIGTERM"',
message: `Command failed: ${cmd}\nInterrupted by ${exitSignal}`,
});
});

Expand Down
18 changes: 13 additions & 5 deletions lib/util/exec/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,31 @@ export function exec(cmd: string, opts: RawExecOptions): Promise<ExecResult> {
// handle process events
cp.on('error', (error) => {
kill(cp, 'SIGTERM');
// rethrowing, use originally emitted error message
reject(new ExecError(error.message, rejectInfo(), error));
});

cp.on('exit', (code: number, signal: NodeJS.Signals) => {
if (NONTERM.includes(signal)) {
return;
}

if (signal) {
const message = `Process signaled with "${signal}"`;
kill(cp, signal);
reject(new ExecError(message, { ...rejectInfo(), signal }));
reject(
new ExecError(`Command failed: ${cmd}\nInterrupted by ${signal}`, {
...rejectInfo(),
signal,
})
);
return;
}
if (code !== 0) {
const message = `Process exited with exit code "${code}"`;
reject(new ExecError(message, { ...rejectInfo(), exitCode: code }));
reject(
new ExecError(`Command failed: ${cmd}\n${stringify(stderr)}`, {
...rejectInfo(),
exitCode: code,
})
);
return;
}
resolve({
Expand Down

0 comments on commit 257d9a9

Please sign in to comment.