ModuleError.js 1.2 KB

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