WebpackError.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // @ts-expect-error ES2018 doesn't `Error.cause`, but it can be used by developers
  21. super(message, options);
  22. /** @type {string=} */
  23. this.details = undefined;
  24. /** @type {(Module | null)=} */
  25. this.module = undefined;
  26. /** @type {DependencyLocation=} */
  27. this.loc = undefined;
  28. /** @type {boolean=} */
  29. this.hideStack = undefined;
  30. /** @type {Chunk=} */
  31. this.chunk = undefined;
  32. /** @type {string=} */
  33. this.file = undefined;
  34. }
  35. [inspect]() {
  36. return (
  37. this.stack +
  38. (this.details ? `\n${this.details}` : "") +
  39. (this.cause ? `\n${this.cause}` : "")
  40. );
  41. }
  42. /**
  43. * @param {ObjectSerializerContext} context context
  44. */
  45. serialize({ write }) {
  46. write(this.name);
  47. write(this.message);
  48. write(this.stack);
  49. write(this.cause);
  50. write(this.details);
  51. write(this.loc);
  52. write(this.hideStack);
  53. }
  54. /**
  55. * @param {ObjectDeserializerContext} context context
  56. */
  57. deserialize({ read }) {
  58. this.name = read();
  59. this.message = read();
  60. this.stack = read();
  61. this.cause = read();
  62. this.details = read();
  63. this.loc = read();
  64. this.hideStack = read();
  65. }
  66. }
  67. makeSerializable(WebpackError, "webpack/lib/WebpackError");
  68. module.exports = WebpackError;