Distinguishing between error strings and error numbers (#187)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent ffc7ca9 · Browse files · 3 files·+45 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+45 added−24 removed
- other:+45 added−24 removed
Provenance
- Commit
7d78db2- Tree
f7848e8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
ffc7ca9
3 files changed+45 added−24 removed
modified:index.js+16 added-10 removed
124 unmodified lines125 const {stdout, stderr, code, signal} = result;126 let {error} = result;127 const {joinedCommand, timedOut, parsed: {options: {timeout}}} = options;128 129- const [codeString, codeNumber] = getCode(result, code);129+ const [exitCodeName, exitCode] = getCode(result, code);130 131 if (!(error instanceof Error)) {132 const message = [joinedCommand, stderr, stdout].filter(Boolean).join('\n');133 error = new Error(message);134 }135 136- const prefix = getErrorPrefix({timedOut, timeout, signal, codeString, codeNumber});136+ const prefix = getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode});137 error.message = `Command ${prefix}: ${error.message}`;138 139- error.code = codeNumber || codeString;139+ error.code = exitCode || exitCodeName;140+ error.exitCode = exitCode;141+ error.exitCodeName = exitCodeName;142 error.stdout = stdout;143 error.stderr = stderr;144 error.failed = true;145 error.signal = signal || null;14 unmodified lines160 161 return [];162 }163 162-function getErrorPrefix({timedOut, timeout, signal, codeString, codeNumber}) {164+function getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode}) {165 if (timedOut) {166 return `timed out after ${timeout} milliseconds`;167 }168 169 if (signal) {170 return `was killed with ${signal}`;171 }172 171- if (codeString !== undefined && codeNumber !== undefined) {172- return `failed with exit code ${codeNumber} (${codeString})`;173+ if (exitCodeName !== undefined && exitCode !== undefined) {174+ return `failed with exit code ${exitCode} (${exitCodeName})`;175 }176 175- if (codeString !== undefined) {176- return `failed with exit code ${codeString}`;177+ if (exitCodeName !== undefined) {178+ return `failed with exit code ${exitCodeName}`;179 }180 179- if (codeNumber !== undefined) {180- return `failed with exit code ${codeNumber}`;181+ if (exitCode !== undefined) {182+ return `failed with exit code ${exitCode}`;183 }184 185 return 'failed';186 }110 unmodified lines297 return {298 stdout: handleOutput(parsed.options, result.stdout),299 stderr: handleOutput(parsed.options, result.stderr),300 code: 0,301+ exitCode: 0,302+ exitCodeName: 'SUCCESS',303 failed: false,304 killed: false,305 signal: null,306 cmd: joinedCommand,58 unmodified lines365 return {366 stdout: handleOutput(parsed.options, result.stdout),367 stderr: handleOutput(parsed.options, result.stderr),368 code: 0,369+ exitCode: 0,370+ exitCodeName: 'SUCCESS',371 failed: false,372 signal: null,373 cmd: joinedCommand,374 timedOut: false4 unmodified linesmodified:readme.md+14 added-8 removed
57 unmodified lines58 } catch (error) {59 console.log(error);60 /*61 {62- message: 'Command failed: /bin/sh -c exit 3'63- killed: false,62+ message: 'Command failed with exit code 3 (ESRCH): exit 3',63 code: 3,65- signal: null,66- cmd: '/bin/sh -c exit 3',64+ exitCode: 3,65+ exitCodeName: 'ESRCH',66 stdout: '',67 stderr: '',68+ failed: true,69+ signal: null,70+ cmd: 'exit 3',69- timedOut: false71+ timedOut: false,72+ killed: false73 }74 */75 }76 })();4 unmodified lines81 } catch (error) {82 console.log(error);83 /*84 {82- message: 'Command failed: /bin/sh -c exit 3'85+ message: 'Command failed with exit code 3 (ESRCH): exit 3',86 code: 3,84- signal: null,85- cmd: '/bin/sh -c exit 3',87+ exitCode: 3,88+ exitCodeName: 'ESRCH',89 stdout: '',90 stderr: '',91+ failed: true,92+ signal: null,93+ cmd: 'exit 3',94 timedOut: false95 }96 */97 }237 unmodified linesmodified:test.js+15 added-6 removed
257 unmodified lines258 t.is(typeof error.stderr, 'string');259 });260 261 test('allow unknown exit code', async t => {262- await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});262+ const {exitCode, exitCodeName} = await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});263+ t.is(exitCode, 255);264+ t.is(exitCodeName, 'Unknown system error -255');265 });266 267 test('execa() returns code and failed properties', async t => {266- const {code, failed} = await execa('noop', ['foo']);267- const error = await t.throwsAsync(execa('exit', ['2']), {code: 2, message: getExitRegExp('2')});268+ const {code, exitCode, exitCodeName, failed} = await execa('noop', ['foo']);269 t.is(code, 0);270+ t.is(exitCode, 0);271+ t.is(exitCodeName, 'SUCCESS');272 t.false(failed);273+274+ const error = await t.throwsAsync(execa('exit', ['2']), {code: 2, message: getExitRegExp('2')});275+ t.is(error.exitCode, 2);276+ const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';277+ t.is(error.exitCodeName, expectedName);278 t.true(error.failed);279 });280 281 test('use relative path with \'..\' chars', async t => {86 unmodified lines368 t.is(error.signal, null);369 });370 371 async function code(t, num) {364- await t.throwsAsync(execa('exit', [`${num}`]), {code: num, message: getExitRegExp(num)});372+ const error = await t.throwsAsync(execa('exit', [`${num}`]), {code: num, message: getExitRegExp(num)});373+ t.is(error.exitCode, num);374 }375 376 test('error.code is 2', code, 2);377 test('error.code is 3', code, 3);2 unmodified lines380 test('timeout will kill the process early', async t => {381 const error = await t.throwsAsync(execa('delay', ['60000', '0'], {timeout: 1500, message: TIMEOUT_REGEXP}));382 383 t.true(error.timedOut);375- t.not(error.code, 22);384+ t.not(error.exitCode, 22);385 });386 387 test('timeout will not kill the process early', async t => {388 const error = await t.throwsAsync(execa('delay', ['3000', '22'], {timeout: 30000}), {code: 22, message: getExitRegExp('22')});80 unmodified lines469 try {470 await execa(`fast-exit-${process.platform}`, [], {input: 'data'});471 t.pass();472 } catch (error) {464- t.is(error.code, 32);473+ t.is(error.exitCode, 32);474 }475 });476 }477 103 unmodified lines