ModuleBuildError.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { cutOffLoaderExecution } = require("./ErrorHelpers");
  7. const WebpackError = require("./WebpackError");
  8. const makeSerializable = require("./util/makeSerializable");
  9. class ModuleBuildError extends WebpackError {
  10. /**
  11. * @param {string | Error&any} err error thrown
  12. * @param {{from?: string|null}} info additional info
  13. */
  14. constructor(err, { from = null } = {}) {
  15. let message = "Module build failed";
  16. let details = undefined;
  17. if (from) {
  18. message += ` (from ${from}):\n`;
  19. } else {
  20. message += ": ";
  21. }
  22. if (err !== null && typeof err === "object") {
  23. if (typeof err.stack === "string" && err.stack) {
  24. const stack = cutOffLoaderExecution(err.stack);
  25. if (!err.hideStack) {
  26. message += stack;
  27. } else {
  28. details = stack;
  29. if (typeof err.message === "string" && err.message) {
  30. message += err.message;
  31. } else {
  32. message += err;
  33. }
  34. }
  35. } else if (typeof err.message === "string" && err.message) {
  36. message += err.message;
  37. } else {
  38. message += String(err);
  39. }
  40. } else {
  41. message += String(err);
  42. }
  43. super(message);
  44. this.name = "ModuleBuildError";
  45. this.details = details;
  46. this.error = err;
  47. }
  48. serialize(context) {
  49. const { write } = context;
  50. write(this.error);
  51. super.serialize(context);
  52. }
  53. deserialize(context) {
  54. const { read } = context;
  55. this.error = read();
  56. super.deserialize(context);
  57. }
  58. }
  59. makeSerializable(ModuleBuildError, "webpack/lib/ModuleBuildError");
  60. module.exports = ModuleBuildError;