Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function formatError(err: Error): string {
if (!err.stack) return err.message;
const stack = cleanStack(err.stack, { pretty: true });
// Regular expression is from: https://github.com/sindresorhus/extract-stack
const pos = stack.search(/(?:\n {4}at .*)+/);
if (!~pos) return stack;
return stack.substring(0, pos) + chalk.gray(stack.substring(pos));
}
test("should extract stack properly", () => {
const [first, second, ...rest] = cleanStack(err.stack!, {
pretty: true
}).split("\n");
expect(stack).toEqual(
first + "\n" + second + chalk.gray("\n" + rest.join("\n"))
);
});
});
logger.title(
messages.boltVersion(pkg.version),
messages.nodeVersion(process.versions.node),
{ emoji: '⚡️' }
);
processes.handleSignals();
try {
await runCommandFromCli(input, flags);
} catch (err) {
if (err instanceof BoltError) {
logger.error(err.message);
} else {
logger.error(cleanStack(err.stack));
}
if (exit) {
process.exit(1);
} else {
throw err;
}
}
let timing = (Date.now() - start) / 1000;
let rounded = Math.round(timing * 100) / 100;
logger.info(messages.doneInSeconds(rounded), {
emoji: '🏁',
prefix: false
});
export const formatError = (err: Error): string => {
const { name, message, stack, ...errorMetadata } = serializeError(err);
const cleanedStack = cleanStack(stack || '').replace(`${name}: ${message}\n`, '');
const stackWithCodeSnippets = cleanedStack
.split('\n')
.map((stackLine: string, index) => getCodeSnippet(stackLine, index === 0 ? message || '' : ''))
.join('\n');
const metadataArray = Object.entries(errorMetadata || {});
const improvedStackArray = [
chalk.red(`${name}: ${(message || '').replace(cwdRegex, '.')}`),
'',
stackWithCodeSnippets,
metadataArray.length ? ' ' : '',
metadataArray.map(([key, value]) => chalk`{gray ${key}}: ${formatAny(value)}`).join('\n')
];
return improvedStackArray
export function logError(error: Error) {
const {name, message, stack} = error;
log(formatErrorMarker(name), ': ', message);
error.stack = cleanStack(stack);
forEach(ErrorStackParser.parse(error), ({functionName = 'unknown', fileName, lineNumber, columnNumber}) => {
log(' • ', ...formatLine(functionName, fileName, lineNumber, columnNumber));
});
}
export const serializeError = (err: Error | unknown) => {
const errorObject = serializeErrorToObject(err);
return {
...errorObject,
message: (errorObject.message || '').replace(cwdRegex, '.'),
stack: cleanStack(errorObject.stack || '').replace(cwdRegex, '.')
};
};