From 76cf34533584f7f69056f70d004931d418900c3d Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 13 Oct 2019 23:44:49 +0200 Subject: [PATCH 1/7] Add `error.signalDescription` --- index.d.ts | 7 ++++++- index.test-d.ts | 4 ++++ lib/error.js | 10 +++++++--- package.json | 1 + readme.md | 8 +++++++- test/error.js | 14 ++++++++++++++ 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index f787fe7566..39ef5843ac 100644 --- a/index.d.ts +++ b/index.d.ts @@ -267,9 +267,14 @@ declare namespace execa { killed: boolean; /** - The signal that was used to terminate the process. + The name of the signal that was used to terminate the process. */ signal?: string; + + /** + A human-friendly description of the signal that was used to terminate the process. + */ + signalDescription?: string; } interface ExecaSyncReturnValue diff --git a/index.test-d.ts b/index.test-d.ts index 5729538879..8d5a5fbc86 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -25,6 +25,7 @@ try { expectType(unicornsResult.isCanceled); expectType(unicornsResult.killed); expectType(unicornsResult.signal); + expectType(unicornsResult.signalDescription); } catch (error) { const execaError: ExecaError = error; @@ -38,6 +39,7 @@ try { expectType(execaError.isCanceled); expectType(execaError.killed); expectType(execaError.signal); + expectType(execaError.signalDescription); expectType(execaError.originalMessage); } @@ -53,6 +55,7 @@ try { expectError(unicornsResult.isCanceled); expectType(unicornsResult.killed); expectType(unicornsResult.signal); + expectType(unicornsResult.signalDescription); } catch (error) { const execaError: ExecaSyncError = error; @@ -66,6 +69,7 @@ try { expectError(execaError.isCanceled); expectType(execaError.killed); expectType(execaError.signal); + expectType(execaError.signalDescription); expectType(execaError.originalMessage); } diff --git a/lib/error.js b/lib/error.js index b651823832..808ad4384b 100644 --- a/lib/error.js +++ b/lib/error.js @@ -1,5 +1,7 @@ 'use strict'; -const getErrorPrefix = ({timedOut, timeout, errorCode, signal, exitCode, isCanceled}) => { +const {signalsByName} = require('human-signals'); + +const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => { if (timedOut) { return `timed out after ${timeout} milliseconds`; } @@ -13,7 +15,7 @@ const getErrorPrefix = ({timedOut, timeout, errorCode, signal, exitCode, isCance } if (signal !== undefined) { - return `was killed with ${signal}`; + return `was killed with ${signal} (${signalDescription})`; } if (exitCode !== undefined) { @@ -40,10 +42,11 @@ const makeError = ({ // We normalize them to `undefined` exitCode = exitCode === null ? undefined : exitCode; signal = signal === null ? undefined : signal; + const signalDescription = signal === undefined ? undefined : signalsByName[signal].description; const errorCode = error && error.code; - const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, exitCode, isCanceled}); + const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}); const message = `Command ${prefix}: ${command}`; if (error instanceof Error) { @@ -56,6 +59,7 @@ const makeError = ({ error.command = command; error.exitCode = exitCode; error.signal = signal; + error.signalDescription = signalDescription; error.stdout = stdout; error.stderr = stderr; diff --git a/package.json b/package.json index b87ea38f07..da1d6a728d 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "dependencies": { "cross-spawn": "^7.0.0", "get-stream": "^5.0.0", + "human-signals": "^1.1.0", "is-stream": "^2.0.0", "merge-stream": "^2.0.0", "npm-run-path": "^4.0.0", diff --git a/readme.md b/readme.md index 69cb1fcf53..2336c0175d 100644 --- a/readme.md +++ b/readme.md @@ -270,7 +270,13 @@ Whether the process was killed. Type: `string | undefined` -The signal that was used to terminate the process. +The name of the signal that was used to terminate the process. + +#### signalDescription + +Type: `string | undefined` + +A human-friendly description of the signal that was used to terminate the process. #### originalMessage diff --git a/test/error.js b/test/error.js index a16581342b..878aaae840 100644 --- a/test/error.js +++ b/test/error.js @@ -133,6 +133,15 @@ if (process.platform !== 'win32') { t.is(signal, 'SIGINT'); }); + test('error.signalDescription is defined', async t => { + const cp = execa('noop'); + + process.kill(cp.pid, 'SIGINT'); + + const {signalDescription} = await t.throwsAsync(cp, {message: /User interruption with CTRL-C/}); + t.is(signalDescription, 'User interruption with CTRL-C'); + }); + test('error.signal is SIGTERM', async t => { const subprocess = execa('noop'); @@ -167,6 +176,11 @@ test('result.signal is undefined if process failed, but was not killed', async t t.is(signal, undefined); }); +test('result.signalDescription is undefined for successful execution', async t => { + const {signalDescription} = await execa('noop'); + t.is(signalDescription, undefined); +}); + test('error.code is undefined on success', async t => { const {code} = await execa('noop'); t.is(code, undefined); From ee772537b8749d4977c5b33fa6dcaa2d41b41022 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 13 Oct 2019 13:03:03 +0200 Subject: [PATCH 2/7] Remove core-js dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da1d6a728d..ce8f1a4f5f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "dependencies": { "cross-spawn": "^7.0.0", "get-stream": "^5.0.0", - "human-signals": "^1.1.0", + "human-signals": "^1.1.1", "is-stream": "^2.0.0", "merge-stream": "^2.0.0", "npm-run-path": "^4.0.0", From 6608ed5d54d5717b4ea083fa30cf4de09e002772 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 13 Oct 2019 13:03:14 +0200 Subject: [PATCH 3/7] Rename `cp` to `subprocess` --- test/error.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/error.js b/test/error.js index 878aaae840..7a79312c52 100644 --- a/test/error.js +++ b/test/error.js @@ -134,11 +134,11 @@ if (process.platform !== 'win32') { }); test('error.signalDescription is defined', async t => { - const cp = execa('noop'); + const subprocess = execa('noop'); - process.kill(cp.pid, 'SIGINT'); + process.kill(subprocess.pid, 'SIGINT'); - const {signalDescription} = await t.throwsAsync(cp, {message: /User interruption with CTRL-C/}); + const {signalDescription} = await t.throwsAsync(subprocess, {message: /User interruption with CTRL-C/}); t.is(signalDescription, 'User interruption with CTRL-C'); }); From 2170454b6b7fda50cad22b9f64f62429789412f0 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 13 Oct 2019 13:04:50 +0200 Subject: [PATCH 4/7] Fix documentation --- index.d.ts | 8 ++++++-- readme.md | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 39ef5843ac..0f547159bf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -267,12 +267,16 @@ declare namespace execa { killed: boolean; /** - The name of the signal that was used to terminate the process. + The name of the signal that was used to terminate the process. For example `"SIGFPE"`. + + If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. */ signal?: string; /** - A human-friendly description of the signal that was used to terminate the process. + A human-friendly description of the signal that was used to terminate the process. For example `"Floating point arithmetic error"`. + + If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. */ signalDescription?: string; } diff --git a/readme.md b/readme.md index 2336c0175d..100416e080 100644 --- a/readme.md +++ b/readme.md @@ -270,13 +270,17 @@ Whether the process was killed. Type: `string | undefined` -The name of the signal that was used to terminate the process. +The name of the signal that was used to terminate the process. For example `"SIGFPE"`. + +If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. #### signalDescription Type: `string | undefined` -A human-friendly description of the signal that was used to terminate the process. +A human-friendly description of the signal that was used to terminate the process. For example `"Floating point arithmetic error"`. + +If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. #### originalMessage From 27feee8b2516fae4849015db015111de59204b29 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 17 Oct 2019 12:51:20 +0700 Subject: [PATCH 5/7] Update index.d.ts --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0f547159bf..9146047cc7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -267,14 +267,14 @@ declare namespace execa { killed: boolean; /** - The name of the signal that was used to terminate the process. For example `"SIGFPE"`. + The name of the signal that was used to terminate the process. For example, `SIGFPE`. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. */ signal?: string; /** - A human-friendly description of the signal that was used to terminate the process. For example `"Floating point arithmetic error"`. + A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. */ From 8a089066cdf133f17a9281f42e0df3f1f6740caa Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 17 Oct 2019 12:51:55 +0700 Subject: [PATCH 6/7] Update readme.md --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 100416e080..e021041f5d 100644 --- a/readme.md +++ b/readme.md @@ -270,7 +270,7 @@ Whether the process was killed. Type: `string | undefined` -The name of the signal that was used to terminate the process. For example `"SIGFPE"`. +The name of the signal that was used to terminate the process. For example, `SIGFPE`. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. @@ -278,7 +278,7 @@ If a signal terminated the process, this property is defined and included in the Type: `string | undefined` -A human-friendly description of the signal that was used to terminate the process. For example `"Floating point arithmetic error"`. +A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. From cf3161a09dcff8a38fb9f31681c3af077090988a Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 13 Oct 2019 14:57:23 +0200 Subject: [PATCH 7/7] Update description --- index.d.ts | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9146047cc7..a615237deb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -276,7 +276,7 @@ declare namespace execa { /** A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`. - If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. + If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen. */ signalDescription?: string; } diff --git a/readme.md b/readme.md index e021041f5d..18f5099318 100644 --- a/readme.md +++ b/readme.md @@ -280,7 +280,7 @@ Type: `string | undefined` A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`. -If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. +If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen. #### originalMessage