Skip to content

Commit

Permalink
Fix error.originalMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Apr 21, 2024
1 parent fdd8a9f commit 51ac58d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/return/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const createMessages = ({
isCanceled,
});
const originalMessage = getOriginalMessage(originalError, cwd);
const newline = originalMessage === '' ? '' : '\n';
const shortMessage = `${prefix}: ${escapedCommand}${newline}${originalMessage}`;
const suffix = originalMessage === undefined ? '' : `\n${originalMessage}`;
const shortMessage = `${prefix}: ${escapedCommand}${suffix}`;
const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];
const message = [shortMessage, ...messageStdio, ...stdio.slice(3)]
.map(messagePart => escapeLines(stripFinalNewline(serializeMessagePart(messagePart))))
Expand Down Expand Up @@ -75,13 +75,14 @@ const getErrorPrefix = ({originalError, timedOut, timeout, isMaxBuffer, maxBuffe

const getOriginalMessage = (originalError, cwd) => {
if (originalError instanceof DiscardedError) {
return '';
return;
}

const originalMessage = isExecaError(originalError)
? originalError.originalMessage
: String(originalError?.message ?? originalError);
return escapeLines(fixCwdError(originalMessage, cwd));
const escapedOriginalMessage = escapeLines(fixCwdError(originalMessage, cwd));
return escapedOriginalMessage === '' ? undefined : escapedOriginalMessage;
};

const serializeMessagePart = messagePart => Array.isArray(messagePart)
Expand Down
5 changes: 4 additions & 1 deletion lib/return/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export const makeError = ({
const error = getFinalError(originalError, message, isSync);

error.shortMessage = shortMessage;
error.originalMessage = originalMessage;
if (originalMessage !== undefined) {
error.originalMessage = originalMessage;
}

error.command = command;
error.escapedCommand = escapedCommand;
error.cwd = cwd;
Expand Down
2 changes: 1 addition & 1 deletion test/resolve/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const testExitCode = async (t, expectedExitCode) => {
execa('exit.js', [`${expectedExitCode}`]),
);
t.is(exitCode, expectedExitCode);
t.is(originalMessage, '');
t.is(originalMessage, undefined);
t.is(shortMessage, `Command failed with exit code ${expectedExitCode}: exit.js ${expectedExitCode}`);
t.is(message, shortMessage);
};
Expand Down
2 changes: 1 addition & 1 deletion test/return/final-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const testUnusualError = async (t, error, expectedOriginalMessage = String(error
const subprocess = execa('empty.js');
subprocess.emit('error', error);
const {originalMessage, shortMessage, message} = await t.throwsAsync(subprocess);
t.is(originalMessage, expectedOriginalMessage);
t.is(originalMessage, expectedOriginalMessage === '' ? undefined : expectedOriginalMessage);
t.true(shortMessage.includes(expectedOriginalMessage));
t.is(message, shortMessage);
};
Expand Down
3 changes: 1 addition & 2 deletions test/return/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const testErrorShape = async (t, execaMethod) => {
'stack',
'message',
'shortMessage',
'originalMessage',
'command',
'escapedCommand',
'cwd',
Expand Down Expand Up @@ -82,7 +81,7 @@ test('error.isTerminated is true if subprocess was killed directly', async t =>
const {isTerminated, signal, originalMessage, message, shortMessage} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});
t.true(isTerminated);
t.is(signal, 'SIGINT');
t.is(originalMessage, '');
t.is(originalMessage, undefined);
t.is(shortMessage, 'Command was killed with SIGINT (User interruption with CTRL-C): forever.js');
t.is(message, shortMessage);
});
Expand Down
2 changes: 1 addition & 1 deletion test/terminate/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('timeout kills the subprocess if it times out', async t => {
t.true(isTerminated);
t.is(signal, 'SIGTERM');
t.true(timedOut);
t.is(originalMessage, '');
t.is(originalMessage, undefined);
t.is(shortMessage, 'Command timed out after 1 milliseconds: forever.js');
t.is(message, shortMessage);
});
Expand Down

0 comments on commit 51ac58d

Please sign in to comment.