Improved error message when spawning commands fails with stderr output

This commit is contained in:
Miro Spönemann 2020-01-22 16:02:00 +01:00
parent fe3cc1904c
commit 8a78e09c6d

View File

@ -33,25 +33,33 @@ export async function getExecPath(commandName: string, logger: ILogger, versionA
export function spawnCommand(command: string, args: string[], logger: ILogger): Promise<string> {
return new Promise<string>((resolve, reject) => {
const cp = spawn(command, args, { windowsHide: true, shell: true });
const buffers: Buffer[] = [];
cp.stdout.on('data', (b: Buffer) => buffers.push(b));
const outBuffers: Buffer[] = [];
const errBuffers: Buffer[] = [];
cp.stdout.on('data', (b: Buffer) => outBuffers.push(b));
cp.stderr.on('data', (b: Buffer) => errBuffers.push(b));
cp.on('error', error => {
logger.error(`Error executing ${command} with args: ${JSON.stringify(args)}.`, error);
logger.error(`Error executing ${command} ${args.join(' ')}`, error);
reject(error);
});
cp.on('exit', (code, signal) => {
if (code === 0) {
const result = Buffer.concat(buffers).toString('utf8').trim()
const result = Buffer.concat(outBuffers).toString('utf8').trim()
resolve(result);
return;
}
if (errBuffers.length > 0) {
const message = Buffer.concat(errBuffers).toString('utf8').trim();
logger.error(`Error executing ${command} ${args.join(' ')}: ${message}`);
reject(new Error(`Process failed with error: ${message}`));
return;
}
if (signal) {
logger.error(`Unexpected signal '${signal}' when executing ${command} with args: ${JSON.stringify(args)}.`);
logger.error(`Unexpected signal '${signal}' when executing ${command} ${args.join(' ')}`);
reject(new Error(`Process exited with signal: ${signal}`));
return;
}
if (code) {
logger.error(`Unexpected exit code '${code}' when executing ${command} with args: ${JSON.stringify(args)}.`);
logger.error(`Unexpected exit code '${code}' when executing ${command} ${args.join(' ')}`);
reject(new Error(`Process exited with exit code: ${code}`));
return;
}