ModuleError.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (from) {
  19. message += ` (from ${from}):\n`;
  20. } else {
  21. message += ": ";
  22. }
  23. if (err && typeof err === "object" && err.message) {
  24. message += err.message;
  25. } else if (err) {
  26. message += err;
  27. }
  28. super(message);
  29. this.name = "ModuleError";
  30. this.error = err;
  31. this.details =
  32. err && typeof err === "object" && err.stack
  33. ? cleanUp(err.stack, this.message)
  34. : undefined;
  35. }
  36. /**
  37. * @param {ObjectSerializerContext} context context
  38. */
  39. serialize(context) {
  40. const { write } = context;
  41. write(this.error);
  42. super.serialize(context);
  43. }
  44. /**
  45. * @param {ObjectDeserializerContext} context context
  46. */
  47. deserialize(context) {
  48. const { read } = context;
  49. this.error = read();
  50. super.deserialize(context);
  51. }
  52. }
  53. makeSerializable(ModuleError, "webpack/lib/ModuleError");
  54. module.exports = ModuleError;