Skip to content

Commit

Permalink
Add error.originalMessage property (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Oct 8, 2019
1 parent 492dff4 commit 07d2b76
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ declare namespace execa {
The error message.
*/
message: string;

/**
Original error message. This is `undefined` unless the child process exited due to an `error` event or a timeout.
The `message` property contains both the `originalMessage` and some additional information added by Execa.
*/
originalMessage?: string;
}

interface ExecaError<StdoutErrorType = string>
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ try {
expectType<boolean>(execaError.isCanceled);
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.originalMessage);
}

try {
Expand Down Expand Up @@ -69,6 +70,7 @@ try {
expectError(execaError.isCanceled);
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.originalMessage);
}

execa('unicorns', {cleanup: false});
Expand Down
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const makeError = ({
const message = `Command ${prefix}: ${command}`;

if (error instanceof Error) {
error.originalMessage = error.message;
error.message = `${message}\n${error.message}`;
} else {
error = new Error(message);
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ Type: `string | undefined`

The signal that was used to terminate the process.

#### originalMessage

Type: `string | undefined`

Original error message. This is `undefined` unless the child process exited due to an `error` event or a timeout.

The `message` property contains both the `originalMessage` and some additional information added by Execa.

### options

Type: `object`
Expand Down
5 changes: 5 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ errorMessage.title = (message, expected) => `error.message matches: ${expected}`

test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');
test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');

test('Original error message is kept', async t => {
const {originalMessage} = await t.throwsAsync(execa('wrong command'));
t.is(originalMessage, 'spawn wrong command ENOENT');
});

0 comments on commit 07d2b76

Please sign in to comment.