Skip to content

Commit

Permalink
Merge pull request #16893 from webpack/thelarkinn/refactor-error-helpers
Browse files Browse the repository at this point in the history
refactor(ErrorHelpers): Make ErrorHelpers named functions for debuggability; add types & docs
  • Loading branch information
TheLarkInn committed Mar 31, 2023
2 parents d96ce76 + 30e9d70 commit 46a3e3d
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions lib/ErrorHelpers.js
Expand Up @@ -9,36 +9,57 @@ const loaderFlag = "LOADER_EXECUTION";

const webpackOptionsFlag = "WEBPACK_OPTIONS";

exports.cutOffByFlag = (stack, flag) => {
stack = stack.split("\n");
for (let i = 0; i < stack.length; i++) {
if (stack[i].includes(flag)) {
stack.length = i;
/**
* @param {string} stack stack trace
* @param {string} flag flag to cut off
* @returns {string} stack trace without the specified flag included
*/
const cutOffByFlag = (stack, flag) => {
const errorStack = stack.split("\n");
for (let i = 0; i < errorStack.length; i++) {
if (errorStack[i].includes(flag)) {
errorStack.length = i;
}
}
return stack.join("\n");
return errorStack.join("\n");
};

exports.cutOffLoaderExecution = stack =>
exports.cutOffByFlag(stack, loaderFlag);
/**
* @param {string} stack stack trace
* @returns {string} stack trace without the loader execution flag included
*/
const cutOffLoaderExecution = stack => cutOffByFlag(stack, loaderFlag);

exports.cutOffWebpackOptions = stack =>
exports.cutOffByFlag(stack, webpackOptionsFlag);
/**
* @param {string} stack stack trace
* @returns {string} stack trace without the webpack options flag included
*/
const cutOffWebpackOptions = stack => cutOffByFlag(stack, webpackOptionsFlag);

exports.cutOffMultilineMessage = (stack, message) => {
stack = stack.split("\n");
message = message.split("\n");
/**
* @param {string} stack stack trace
* @param {string} message error message
* @returns {string} stack trace without the message included
*/
const cutOffMultilineMessage = (stack, message) => {
const stackSplitByLines = stack.split("\n");
const messageSplitByLines = message.split("\n");

const result = [];

stack.forEach((line, idx) => {
if (!line.includes(message[idx])) result.push(line);
stackSplitByLines.forEach((line, idx) => {
if (!line.includes(messageSplitByLines[idx])) result.push(line);
});

return result.join("\n");
};

exports.cutOffMessage = (stack, message) => {
/**
* @param {string} stack stack trace
* @param {string} message error message
* @returns {string} stack trace without the message included
*/
const cutOffMessage = (stack, message) => {
const nextLine = stack.indexOf("\n");
if (nextLine === -1) {
return stack === message ? "" : stack;
Expand All @@ -48,14 +69,32 @@ exports.cutOffMessage = (stack, message) => {
}
};

exports.cleanUp = (stack, message) => {
stack = exports.cutOffLoaderExecution(stack);
stack = exports.cutOffMessage(stack, message);
/**
* @param {string} stack stack trace
* @param {string} message error message
* @returns {string} stack trace without the loader execution flag and message included
*/
const cleanUp = (stack, message) => {
stack = cutOffLoaderExecution(stack);
stack = cutOffMessage(stack, message);
return stack;
};

exports.cleanUpWebpackOptions = (stack, message) => {
stack = exports.cutOffWebpackOptions(stack);
stack = exports.cutOffMultilineMessage(stack, message);
/**
* @param {string} stack stack trace
* @param {string} message error message
* @returns {string} stack trace without the webpack options flag and message included
*/
const cleanUpWebpackOptions = (stack, message) => {
stack = cutOffWebpackOptions(stack);
stack = cutOffMultilineMessage(stack, message);
return stack;
};

exports.cutOffByFlag = cutOffByFlag;
exports.cutOffLoaderExecution = cutOffLoaderExecution;
exports.cutOffWebpackOptions = cutOffWebpackOptions;
exports.cutOffMultilineMessage = cutOffMultilineMessage;
exports.cutOffMessage = cutOffMessage;
exports.cleanUp = cleanUp;
exports.cleanUpWebpackOptions = cleanUpWebpackOptions;

0 comments on commit 46a3e3d

Please sign in to comment.