WebpackError.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /** @import Chunk from "../Chunk" */
  9. /** @import { DependencyLocation } from "../Dependency" */
  10. /** @import Module from "../Module" */
  11. /**
  12. * @import {
  13. * ObjectDeserializerContext,
  14. * ObjectSerializerContext
  15. * } from "../serialization/ObjectMiddleware"
  16. */
  17. class WebpackError extends Error {
  18. /**
  19. * Creates an instance of WebpackError.
  20. * @param {string=} message error message
  21. * @param {{ cause?: unknown }} options error options
  22. */
  23. constructor(message, options = {}) {
  24. super(message, options);
  25. /** @type {string=} */
  26. this.details = undefined;
  27. /** @type {(Module | null)=} */
  28. this.module = undefined;
  29. /** @type {DependencyLocation=} */
  30. this.loc = undefined;
  31. /** @type {boolean=} */
  32. this.hideStack = undefined;
  33. /** @type {Chunk=} */
  34. this.chunk = undefined;
  35. /** @type {string=} */
  36. this.file = undefined;
  37. }
  38. /**
  39. * Returns inspect message.
  40. * @returns {string} inspect message
  41. */
  42. [inspect]() {
  43. return (
  44. this.stack +
  45. (this.details ? `\n${this.details}` : "") +
  46. (this.cause ? `\n${this.cause}` : "")
  47. );
  48. }
  49. /**
  50. * Serializes this instance into the provided serializer context.
  51. * @param {ObjectSerializerContext} context context
  52. */
  53. serialize({ write }) {
  54. write(this.name);
  55. write(this.message);
  56. write(this.stack);
  57. write(this.cause);
  58. write(this.details);
  59. write(this.loc);
  60. write(this.hideStack);
  61. }
  62. /**
  63. * Restores this instance from the provided deserializer context.
  64. * @param {ObjectDeserializerContext} context context
  65. */
  66. deserialize({ read }) {
  67. this.name = read();
  68. this.message = read();
  69. this.stack = read();
  70. this.cause = read();
  71. this.details = read();
  72. this.loc = read();
  73. this.hideStack = read();
  74. }
  75. }
  76. makeSerializable(WebpackError, "webpack/lib/errors/WebpackError");
  77. /** @type {typeof WebpackError} */
  78. module.exports = WebpackError;