WebpackError.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Jarid Margolin @jaridmargolin
  4. */
  5. "use strict";
  6. const inspect = require("util").inspect.custom;
  7. const makeSerializable = require("./util/makeSerializable");
  8. /** @typedef {import("./Chunk")} Chunk */
  9. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  10. /** @typedef {import("./Module")} Module */
  11. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  12. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  13. class WebpackError extends Error {
  14. /**
  15. * Creates an instance of WebpackError.
  16. * @param {string=} message error message
  17. * @param {{ cause?: unknown }} options error options
  18. */
  19. constructor(message, options = {}) {
  20. super(message, options);
  21. /** @type {string=} */
  22. this.details = undefined;
  23. /** @type {(Module | null)=} */
  24. this.module = undefined;
  25. /** @type {DependencyLocation=} */
  26. this.loc = undefined;
  27. /** @type {boolean=} */
  28. this.hideStack = undefined;
  29. /** @type {Chunk=} */
  30. this.chunk = undefined;
  31. /** @type {string=} */
  32. this.file = undefined;
  33. }
  34. /**
  35. * Returns inspect message.
  36. * @returns {string} inspect message
  37. */
  38. [inspect]() {
  39. return (
  40. this.stack +
  41. (this.details ? `\n${this.details}` : "") +
  42. (this.cause ? `\n${this.cause}` : "")
  43. );
  44. }
  45. /**
  46. * Serializes this instance into the provided serializer context.
  47. * @param {ObjectSerializerContext} context context
  48. */
  49. serialize({ write }) {
  50. write(this.name);
  51. write(this.message);
  52. write(this.stack);
  53. write(this.cause);
  54. write(this.details);
  55. write(this.loc);
  56. write(this.hideStack);
  57. }
  58. /**
  59. * Restores this instance from the provided deserializer context.
  60. * @param {ObjectDeserializerContext} context context
  61. */
  62. deserialize({ read }) {
  63. this.name = read();
  64. this.message = read();
  65. this.stack = read();
  66. this.cause = read();
  67. this.details = read();
  68. this.loc = read();
  69. this.hideStack = read();
  70. }
  71. }
  72. makeSerializable(WebpackError, "webpack/lib/WebpackError");
  73. /** @type {typeof WebpackError} */
  74. module.exports = WebpackError;