ErrorHelpers.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const loaderFlag = "LOADER_EXECUTION";
  7. const webpackOptionsFlag = "WEBPACK_OPTIONS";
  8. /**
  9. * Returns stack trace without the specified flag included.
  10. * @param {string} stack stack trace
  11. * @param {string} flag flag to cut off
  12. * @returns {string} stack trace without the specified flag included
  13. */
  14. const cutOffByFlag = (stack, flag) => {
  15. const errorStack = stack.split("\n");
  16. for (let i = 0; i < errorStack.length; i++) {
  17. if (errorStack[i].includes(flag)) {
  18. errorStack.length = i;
  19. }
  20. }
  21. return errorStack.join("\n");
  22. };
  23. /**
  24. * Cut off loader execution.
  25. * @param {string} stack stack trace
  26. * @returns {string} stack trace without the loader execution flag included
  27. */
  28. const cutOffLoaderExecution = (stack) => cutOffByFlag(stack, loaderFlag);
  29. /**
  30. * Cut off webpack options.
  31. * @param {string} stack stack trace
  32. * @returns {string} stack trace without the webpack options flag included
  33. */
  34. const cutOffWebpackOptions = (stack) => cutOffByFlag(stack, webpackOptionsFlag);
  35. /**
  36. * Cut off multiline message.
  37. * @param {string} stack stack trace
  38. * @param {string} message error message
  39. * @returns {string} stack trace without the message included
  40. */
  41. const cutOffMultilineMessage = (stack, message) => {
  42. const stackSplitByLines = stack.split("\n");
  43. const messageSplitByLines = message.split("\n");
  44. /** @type {string[]} */
  45. const result = [];
  46. for (const [idx, line] of stackSplitByLines.entries()) {
  47. if (!line.includes(messageSplitByLines[idx])) result.push(line);
  48. }
  49. return result.join("\n");
  50. };
  51. /**
  52. * Returns stack trace without the message included.
  53. * @param {string} stack stack trace
  54. * @param {string} message error message
  55. * @returns {string} stack trace without the message included
  56. */
  57. const cutOffMessage = (stack, message) => {
  58. const nextLine = stack.indexOf("\n");
  59. if (nextLine === -1) {
  60. return stack === message ? "" : stack;
  61. }
  62. const firstLine = stack.slice(0, nextLine);
  63. return firstLine === message ? stack.slice(nextLine + 1) : stack;
  64. };
  65. /**
  66. * Returns stack trace without the loader execution flag and message included.
  67. * @param {string} stack stack trace
  68. * @param {string} message error message
  69. * @returns {string} stack trace without the loader execution flag and message included
  70. */
  71. const cleanUp = (stack, message) => {
  72. stack = cutOffLoaderExecution(stack);
  73. stack = cutOffMessage(stack, message);
  74. return stack;
  75. };
  76. /**
  77. * Clean up webpack options.
  78. * @param {string} stack stack trace
  79. * @param {string} message error message
  80. * @returns {string} stack trace without the webpack options flag and message included
  81. */
  82. const cleanUpWebpackOptions = (stack, message) => {
  83. stack = cutOffWebpackOptions(stack);
  84. stack = cutOffMultilineMessage(stack, message);
  85. return stack;
  86. };
  87. module.exports.cleanUp = cleanUp;
  88. module.exports.cleanUpWebpackOptions = cleanUpWebpackOptions;
  89. module.exports.cutOffByFlag = cutOffByFlag;
  90. module.exports.cutOffLoaderExecution = cutOffLoaderExecution;
  91. module.exports.cutOffMessage = cutOffMessage;
  92. module.exports.cutOffMultilineMessage = cutOffMultilineMessage;
  93. module.exports.cutOffWebpackOptions = cutOffWebpackOptions;