message.js
This file
Path
lib/return/message.js
102 lines · 2.8 KiB · JavaScript
Last changed by
Fix `error.originalMessage` (#991)ehmickybefore adoptionc78ae5c
History of this fileTake it
Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 1why, one step back1import stripFinalNewline from 'strip-final-newline';Rename files (#977)ehmickybefore adoption4b2e316
line 2why, one step back2import {isUint8Array, uint8ArrayToString} from '../utils/uint-array.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 3–4why, one step back3import {fixCwdError} from '../arguments/cwd.js';4import {escapeLines} from '../arguments/escape.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 5–6why, one step back5import {getMaxBufferMessage} from '../io/max-buffer.js';6import {DiscardedError, isExecaError} from './final-error.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 7why, one step back7Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 8why, one step back8// Computes `error.message`, `error.shortMessage` and `error.originalMessage`Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 9–24why, one step back9export const createMessages = ({10 stdio,11 all,12 originalError,13 signal,14 signalDescription,15 exitCode,16 escapedCommand,17 timedOut,18 isCanceled,19 isMaxBuffer,20 maxBuffer,21 timeout,22 cwd,23}) => {24 const errorCode = originalError?.code;Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 25–36why, one step back25 const prefix = getErrorPrefix({26 originalError,27 timedOut,28 timeout,29 isMaxBuffer,30 maxBuffer,31 errorCode,32 signal,33 signalDescription,34 exitCode,35 isCanceled,36 });Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 37why, one step back37 const originalMessage = getOriginalMessage(originalError, cwd);Fix `error.originalMessage` (#991)ehmickybefore adoptionc78ae5c
lines 38–39why, one step back38 const suffix = originalMessage === undefined ? '' : `\n${originalMessage}`;39 const shortMessage = `${prefix}: ${escapedCommand}${suffix}`;Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 40–77why, one step back40 const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];41 const message = [shortMessage, ...messageStdio, ...stdio.slice(3)]42 .map(messagePart => escapeLines(stripFinalNewline(serializeMessagePart(messagePart))))43 .filter(Boolean)44 .join('\n\n');45 return {originalMessage, shortMessage, message};46};4748const getErrorPrefix = ({originalError, timedOut, timeout, isMaxBuffer, maxBuffer, errorCode, signal, signalDescription, exitCode, isCanceled}) => {49 if (timedOut) {50 return `Command timed out after ${timeout} milliseconds`;51 }5253 if (isCanceled) {54 return 'Command was canceled';55 }5657 if (isMaxBuffer) {58 return getMaxBufferMessage(originalError, maxBuffer);59 }6061 if (errorCode !== undefined) {62 return `Command failed with ${errorCode}`;63 }6465 if (signal !== undefined) {66 return `Command was killed with ${signal} (${signalDescription})`;67 }6869 if (exitCode !== undefined) {70 return `Command failed with exit code ${exitCode}`;71 }7273 return 'Command failed';74};7576const getOriginalMessage = (originalError, cwd) => {77 if (originalError instanceof DiscardedError) {Fix `error.originalMessage` (#991)ehmickybefore adoptionc78ae5c
line 78why, one step back78 return;Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 79–83why, one step back79 }8081 const originalMessage = isExecaError(originalError)82 ? originalError.originalMessage83 : String(originalError?.message ?? originalError);Fix `error.originalMessage` (#991)ehmickybefore adoptionc78ae5c
lines 84–85why, one step back84 const escapedOriginalMessage = escapeLines(fixCwdError(originalMessage, cwd));85 return escapedOriginalMessage === '' ? undefined : escapedOriginalMessage;Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 86–102why, one step back86};8788const serializeMessagePart = messagePart => Array.isArray(messagePart)89 ? messagePart.map(messageItem => stripFinalNewline(serializeMessageItem(messageItem))).filter(Boolean).join('\n')90 : serializeMessageItem(messagePart);9192const serializeMessageItem = messageItem => {93 if (typeof messageItem === 'string') {94 return messageItem;95 }9697 if (isUint8Array(messageItem)) {98 return uint8ArrayToString(messageItem);99 }100101 return '';102};