Add `error.signalDescription` (#378)
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 1ac56ea · Browse files · 6 files·+47 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+47 added−5 removed
- tests:+14 added−0 removed
- other:+33 added−5 removed
Provenance
- Commit
d8cb08f- Tree
09b8f98- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
1ac56ea
6 files changed+47 added−5 removed
modified:lib/error.js+7 added-3 removed
1232456785 unmodified lines1415161716181920212218 unmodified lines41424344454647484649505152534 unmodified lines58596061626364656616 unmodified lines'use strict';const {signalsByName} = require('human-signals');
const getErrorPrefix = ({timedOut, timeout, errorCode, signal, exitCode, isCanceled}) => {const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => { if (timedOut) { return `timed out after ${timeout} milliseconds`; }
5 unmodified lines return `failed with ${errorCode}`; }
if (signal !== undefined) { return `was killed with ${signal}`; return `was killed with ${signal} (${signalDescription})`; }
if (exitCode !== undefined) { return `failed with exit code ${exitCode}`;18 unmodified lines // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`. // 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) { error.originalMessage = error.message;4 unmodified lines
error.command = command; error.exitCode = exitCode; error.signal = signal; error.signalDescription = signalDescription; error.stdout = stdout; error.stderr = stderr;
if (all !== undefined) {16 unmodified linesmodified:test/error.js+14 added
131 unmodified lines132 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});133 t.is(signal, 'SIGINT');134 });135 136+ test('error.signalDescription is defined', async t => {137+ const subprocess = execa('noop');138+139+ process.kill(subprocess.pid, 'SIGINT');140+141+ const {signalDescription} = await t.throwsAsync(subprocess, {message: /User interruption with CTRL-C/});142+ t.is(signalDescription, 'User interruption with CTRL-C');143+ });144+145 test('error.signal is SIGTERM', async t => {146 const subprocess = execa('noop');147 148 process.kill(subprocess.pid, 'SIGTERM');26 unmodified lines175 const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});176 t.is(signal, undefined);177 });178 179+test('result.signalDescription is undefined for successful execution', async t => {180+ const {signalDescription} = await execa('noop');181+ t.is(signalDescription, undefined);182+});183+184 test('error.code is undefined on success', async t => {185 const {code} = await execa('noop');186 t.is(code, undefined);187 });5 unmodified linesmodified:index.d.ts+10 added-1 removed
265 unmodified lines266 */267 killed: boolean;268 269 /**270- The signal that was used to terminate the process.270+ The name of the signal that was used to terminate the process. For example, `SIGFPE`.271+272+ If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.273 */274 signal?: string;275+276+ /**277+ A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.278+279+ 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.280+ */281+ signalDescription?: string;282 }283 284 interface ExecaSyncReturnValue<StdoutErrorType = string>285 extends ExecaReturnBase<StdoutErrorType> {237 unmodified linesmodified:index.test-d.ts+4 added
23 unmodified lines24 expectType<boolean>(unicornsResult.timedOut);25 expectType<boolean>(unicornsResult.isCanceled);26 expectType<boolean>(unicornsResult.killed);27 expectType<string | undefined>(unicornsResult.signal);28+ expectType<string | undefined>(unicornsResult.signalDescription);29 } catch (error) {30 const execaError: ExecaError = error;31 32 expectType<string>(execaError.message);5 unmodified lines38 expectType<boolean>(execaError.timedOut);39 expectType<boolean>(execaError.isCanceled);40 expectType<boolean>(execaError.killed);41 expectType<string | undefined>(execaError.signal);42+ expectType<string | undefined>(execaError.signalDescription);43 expectType<string | undefined>(execaError.originalMessage);44 }45 46 try {7 unmodified lines54 expectType<boolean>(unicornsResult.timedOut);55 expectError(unicornsResult.isCanceled);56 expectType<boolean>(unicornsResult.killed);57 expectType<string | undefined>(unicornsResult.signal);58+ expectType<string | undefined>(unicornsResult.signalDescription);59 } catch (error) {60 const execaError: ExecaSyncError = error;61 62 expectType<string>(execaError.message);5 unmodified lines68 expectType<boolean>(execaError.timedOut);69 expectError(execaError.isCanceled);70 expectType<boolean>(execaError.killed);71 expectType<string | undefined>(execaError.signal);72+ expectType<string | undefined>(execaError.signalDescription);73 expectType<string | undefined>(execaError.originalMessage);74 }75 76 execa('unicorns', {cleanup: false});111 unmodified linesmodified:package.json+1 added
38 unmodified lines39404142434445464724 unmodified lines38 unmodified lines ], "dependencies": { "cross-spawn": "^7.0.0", "get-stream": "^5.0.0", "human-signals": "^1.1.1", "is-stream": "^2.0.0", "merge-stream": "^2.0.0", "npm-run-path": "^4.0.0", "onetime": "^5.1.0",24 unmodified linesmodified:readme.md+11 added-1 removed
268 unmodified lines269 #### signal270 271 Type: `string | undefined`272 273-The signal that was used to terminate the process.273+The name of the signal that was used to terminate the process. For example, `SIGFPE`.274 275+If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.276+277+#### signalDescription278+279+Type: `string | undefined`280+281+A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.282+283+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.284+285 #### originalMessage286 287 Type: `string | undefined`288 289 unmodified lines