@farmfe/core
Version:
Farm is a extremely fast web build tool written in Rust. Farm can start a project in milliseconds and perform HMR within 10ms, making it much faster than similar tools like webpack and vite.
84 lines • 2.71 kB
JavaScript
import { colors } from '../utils/color.js';
import { pad } from '../utils/share.js';
// import { DevServer } from './index.js';
export function prepareError(err) {
return {
message: stripAnsi(err.message),
stack: stripAnsi(cleanStack(err.stack || '')),
id: err.id,
frame: stripAnsi(err.frame || ''),
plugin: err.plugin,
pluginCode: err.pluginCode?.toString(),
loc: err.loc,
potential: err.potentialSolution || ''
};
}
export function stripAnsi(str) {
// eslint-disable-next-line no-control-regex
return str.replace(/\x1b\[[0-9;]*m/g, '');
}
export function cleanStack(stack) {
return stack
.split(/\n/g)
.filter((l) => /^\s*at/.test(l))
.join('\n');
}
export function buildErrorMessage(err, args = [], includeStack = true) {
if (err.plugin)
args.push(` Plugin: ${colors.magenta(err.plugin)}`);
const loc = err.loc ? `:${err.loc.line}:${err.loc.column}` : '';
if (err.id)
args.push(` File: ${colors.cyan(err.id)}${loc}`);
if (err.frame)
args.push(colors.yellow(pad(err.frame)));
else if (err.source)
args.push(colors.yellow(err.source));
if (includeStack && err.stack)
args.push(pad(cleanStack(err.stack)));
return args.join('\n');
}
export function logError(err, throwErrorFlag = true) {
let errorMessages = [];
try {
errorMessages = JSON.parse(err.message);
}
catch (_) {
throw new Error(err.message);
}
if (!Array.isArray(errorMessages) || errorMessages.length === 0) {
if (throwErrorFlag) {
throw new Error(err.message);
}
return err.message;
}
const formattedErrorMessages = errorMessages.map((errorMsg) => {
try {
const parsedErrorMsg = JSON.parse(errorMsg);
if (parsedErrorMsg &&
typeof parsedErrorMsg === 'object' &&
(parsedErrorMsg.message || parsedErrorMsg.reason)) {
return `${buildErrorMessage(parsedErrorMsg, [
colors.red(`Internal server error: ${parsedErrorMsg.message || parsedErrorMsg.reason}`)
])}`;
}
else {
return colors.red(errorMsg);
}
}
catch {
return colors.red(errorMsg);
}
});
const errorMessage = formattedErrorMessages.join('\n');
if (throwErrorFlag) {
throw new Error(errorMessage);
}
return errorMessage;
}
// TODO server logger e.g: DevServer.logger.error(msg);
// server.ws.send({
// type: 'error',
// err: prepareError(err)
// });
// }
//# sourceMappingURL=error.js.map