Skip to content

Commit

Permalink
Remove error.code (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed May 14, 2019
1 parent fd064a7 commit bc0984e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 deletions.
9 changes: 0 additions & 9 deletions index.d.ts
Expand Up @@ -242,10 +242,6 @@ declare namespace execa {

interface ExecaSyncReturnValue<StdoutErrorType = string>
extends ExecaReturnBase<StdoutErrorType> {
/**
The exit code of the process that was run.
*/
code: number;
}

interface ExecaReturnValue<StdoutErrorType = string>
Expand All @@ -268,11 +264,6 @@ declare namespace execa {
The error message.
*/
message: string;

/**
The exit code (either numeric or textual) of the process that was run.
*/
code: number | string;
}

interface ExecaError<StdoutErrorType = string>
Expand Down
11 changes: 5 additions & 6 deletions index.js
Expand Up @@ -166,9 +166,9 @@ function getStream(process, stream, {encoding, buffer, maxBuffer}) {
}

function makeError(result, options) {
const {stdout, stderr, code, signal} = result;
const {stdout, stderr, signal} = result;
let {error} = result;
const {joinedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout}}} = options;
const {code, joinedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout}}} = options;

const [exitCodeName, exitCode] = getCode(result, code);

Expand All @@ -182,9 +182,9 @@ function makeError(result, options) {
}

error.command = joinedCommand;
delete error.code;
error.exitCode = exitCode;
error.exitCodeName = exitCodeName;
error.code = exitCode || exitCodeName;
error.stdout = stdout;
error.stderr = stderr;

Expand Down Expand Up @@ -356,6 +356,7 @@ const execa = (command, args, options) => {

if (result.error || result.code !== 0 || result.signal !== null) {
const error = makeError(result, {
code: result.code,
joinedCommand,
parsed,
timedOut,
Expand All @@ -374,7 +375,6 @@ const execa = (command, args, options) => {
command: joinedCommand,
exitCode: 0,
exitCodeName: 'SUCCESS',
code: 0,
stdout: result.stdout,
stderr: result.stderr,
all: result.all,
Expand Down Expand Up @@ -423,12 +423,12 @@ module.exports.sync = (command, args, options) => {
}

const result = childProcess.spawnSync(parsed.command, parsed.args, parsed.options);
result.code = result.status;
result.stdout = handleOutput(parsed.options, result.stdout);
result.stderr = handleOutput(parsed.options, result.stderr);

if (result.error || result.status !== 0 || result.signal !== null) {
const error = makeError(result, {
code: result.status,
joinedCommand,
parsed,
timedOut: false,
Expand All @@ -447,7 +447,6 @@ module.exports.sync = (command, args, options) => {
command: joinedCommand,
exitCode: 0,
exitCodeName: 'SUCCESS',
code: 0,
stdout: result.stdout,
stderr: result.stderr,
failed: false,
Expand Down
4 changes: 0 additions & 4 deletions index.test-d.ts
Expand Up @@ -14,7 +14,6 @@ try {

const unicornsResult = await execaPromise;
expectType<string>(unicornsResult.command);
expectType<string | number>(unicornsResult.code);
expectType<number>(unicornsResult.exitCode);
expectType<string>(unicornsResult.exitCodeName);
expectType<string>(unicornsResult.stdout);
Expand All @@ -29,7 +28,6 @@ try {
const execaError: ExecaError = error;

expectType<string>(execaError.message);
expectType<number | string>(execaError.code);
expectType<number>(execaError.exitCode);
expectType<string>(execaError.exitCodeName);
expectType<string>(execaError.stdout);
Expand All @@ -45,7 +43,6 @@ try {
try {
const unicornsResult = execa.sync('unicorns');
expectType<string>(unicornsResult.command);
expectType<string | number>(unicornsResult.code);
expectType<number>(unicornsResult.exitCode);
expectType<string>(unicornsResult.exitCodeName);
expectType<string>(unicornsResult.stdout);
Expand All @@ -60,7 +57,6 @@ try {
const execaError: ExecaSyncError = error;

expectType<string>(execaError.message);
expectType<number | string>(execaError.code);
expectType<number>(execaError.exitCode);
expectType<string>(execaError.exitCodeName);
expectType<string>(execaError.stdout);
Expand Down
2 changes: 0 additions & 2 deletions readme.md
Expand Up @@ -62,7 +62,6 @@ const execa = require('execa');
{
message: 'Command failed with exit code 2 (ENOENT): wrong command spawn wrong ENOENT',
errno: 'ENOENT',
code: 2,
syscall: 'spawn wrong',
path: 'wrong',
spawnargs: ['command'],
Expand Down Expand Up @@ -102,7 +101,6 @@ try {
{
message: 'Command failed with exit code 2 (ENOENT): wrong command spawnSync wrong ENOENT',
errno: 'ENOENT',
code: 2,
syscall: 'spawnSync wrong',
path: 'wrong',
spawnargs: ['command'],
Expand Down
17 changes: 8 additions & 9 deletions test.js
Expand Up @@ -260,13 +260,12 @@ test('allow unknown exit code', async t => {
});

test('execa() returns code and failed properties', async t => {
const {code, exitCode, exitCodeName, failed} = await execa('noop', ['foo']);
t.is(code, 0);
const {exitCode, exitCodeName, failed} = await execa('noop', ['foo']);
t.is(exitCode, 0);
t.is(exitCodeName, 'SUCCESS');
t.false(failed);

const error = await t.throwsAsync(execa('exit', ['2']), {code: 2, message: getExitRegExp('2')});
const error = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(error.exitCode, 2);
const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';
t.is(error.exitCodeName, expectedName);
Expand Down Expand Up @@ -378,14 +377,14 @@ test('result.signal is undefined if process failed, but was not killed', async t
t.is(error.signal, undefined);
});

async function code(t, num) {
const error = await t.throwsAsync(execa('exit', [`${num}`]), {code: num, message: getExitRegExp(num)});
t.is(error.exitCode, num);
async function testExitCode(t, num) {
const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});
t.is(exitCode, num);
}

test('error.code is 2', code, 2);
test('error.code is 3', code, 3);
test('error.code is 4', code, 4);
test('error.exitCode is 2', testExitCode, 2);
test('error.exitCode is 3', testExitCode, 3);
test('error.exitCode is 4', testExitCode, 4);

test('timeout kills the process if it times out', async t => {
const error = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));
Expand Down

0 comments on commit bc0984e

Please sign in to comment.