ModuleError.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  10. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  11. class ModuleError extends WebpackError {
  12. /**
  13. * @param {Error} err error thrown
  14. * @param {{ from?: string | null }} info additional info
  15. */
  16. constructor(err, { from = null } = {}) {
  17. let message = "Module Error";
  18. message += from ? ` (from ${from}):\n` : ": ";
  19. if (err && typeof err === "object" && err.message) {
  20. message += err.message;
  21. } else if (err) {
  22. message += err;
  23. }
  24. super(message);
  25. /** @type {string} */
  26. this.name = "ModuleError";
  27. /** @type {Error} */
  28. this.error = err;
  29. /** @type {string | undefined} */
  30. this.details =
  31. err && typeof err === "object" && err.stack
  32. ? cleanUp(err.stack, this.message)
  33. : undefined;
  34. }
  35. /**
  36. * @param {ObjectSerializerContext} context context
  37. */
  38. serialize(context) {
  39. const { write } = context;
  40. write(this.error);
  41. super.serialize(context);
  42. }
  43. /**
  44. * @param {ObjectDeserializerContext} context context
  45. */
  46. deserialize(context) {
  47. const { read } = context;
  48. this.error = read();
  49. super.deserialize(context);
  50. }
  51. }
  52. makeSerializable(ModuleError, "webpack/lib/ModuleError");
  53. module.exports = ModuleError;