ErrorHelpers.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @param {string} stack stack trace
  10. * @param {string} flag flag to cut off
  11. * @returns {string} stack trace without the specified flag included
  12. */
  13. const cutOffByFlag = (stack, flag) => {
  14. const errorStack = stack.split("\n");
  15. for (let i = 0; i < errorStack.length; i++) {
  16. if (errorStack[i].includes(flag)) {
  17. errorStack.length = i;
  18. }
  19. }
  20. return errorStack.join("\n");
  21. };
  22. /**
  23. * @param {string} stack stack trace
  24. * @returns {string} stack trace without the loader execution flag included
  25. */
  26. const cutOffLoaderExecution = stack => cutOffByFlag(stack, loaderFlag);
  27. /**
  28. * @param {string} stack stack trace
  29. * @returns {string} stack trace without the webpack options flag included
  30. */
  31. const cutOffWebpackOptions = stack => cutOffByFlag(stack, webpackOptionsFlag);
  32. /**
  33. * @param {string} stack stack trace
  34. * @param {string} message error message
  35. * @returns {string} stack trace without the message included
  36. */
  37. const cutOffMultilineMessage = (stack, message) => {
  38. const stackSplitByLines = stack.split("\n");
  39. const messageSplitByLines = message.split("\n");
  40. /** @type {string[]} */
  41. const result = [];
  42. stackSplitByLines.forEach((line, idx) => {
  43. if (!line.includes(messageSplitByLines[idx])) result.push(line);
  44. });
  45. return result.join("\n");
  46. };
  47. /**
  48. * @param {string} stack stack trace
  49. * @param {string} message error message
  50. * @returns {string} stack trace without the message included
  51. */
  52. const cutOffMessage = (stack, message) => {
  53. const nextLine = stack.indexOf("\n");
  54. if (nextLine === -1) {
  55. return stack === message ? "" : stack;
  56. } else {
  57. const firstLine = stack.slice(0, nextLine);
  58. return firstLine === message ? stack.slice(nextLine + 1) : stack;
  59. }
  60. };
  61. /**
  62. * @param {string} stack stack trace
  63. * @param {string} message error message
  64. * @returns {string} stack trace without the loader execution flag and message included
  65. */
  66. const cleanUp = (stack, message) => {
  67. stack = cutOffLoaderExecution(stack);
  68. stack = cutOffMessage(stack, message);
  69. return stack;
  70. };
  71. /**
  72. * @param {string} stack stack trace
  73. * @param {string} message error message
  74. * @returns {string} stack trace without the webpack options flag and message included
  75. */
  76. const cleanUpWebpackOptions = (stack, message) => {
  77. stack = cutOffWebpackOptions(stack);
  78. stack = cutOffMultilineMessage(stack, message);
  79. return stack;
  80. };
  81. exports.cutOffByFlag = cutOffByFlag;
  82. exports.cutOffLoaderExecution = cutOffLoaderExecution;
  83. exports.cutOffWebpackOptions = cutOffWebpackOptions;
  84. exports.cutOffMultilineMessage = cutOffMultilineMessage;
  85. exports.cutOffMessage = cutOffMessage;
  86. exports.cleanUp = cleanUp;
  87. exports.cleanUpWebpackOptions = cleanUpWebpackOptions;