From 8a78e09c6dee4251f98058ee53efa17f36531ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Sp=C3=B6nemann?= Date: Wed, 22 Jan 2020 16:02:00 +0100 Subject: [PATCH] Improved error message when spawning commands fails with stderr output --- arduino-ide-extension/src/node/exec-util.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/arduino-ide-extension/src/node/exec-util.ts b/arduino-ide-extension/src/node/exec-util.ts index cb865602..ab384a88 100644 --- a/arduino-ide-extension/src/node/exec-util.ts +++ b/arduino-ide-extension/src/node/exec-util.ts @@ -33,25 +33,33 @@ export async function getExecPath(commandName: string, logger: ILogger, versionA export function spawnCommand(command: string, args: string[], logger: ILogger): Promise { return new Promise((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; }