HookWebpackError.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sean Larkin @thelarkinn
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const WebpackError = require("./WebpackError");
  8. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Error, string]>} ObjectDeserializerContext */
  9. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Error, string]>} ObjectSerializerContext */
  10. /**
  11. * Defines the callback callback.
  12. * @template T
  13. * @callback Callback
  14. * @param {Error | null} err
  15. * @param {T=} stats
  16. * @returns {void}
  17. */
  18. class HookWebpackError extends WebpackError {
  19. /**
  20. * Creates an instance of HookWebpackError.
  21. * @param {Error} error inner error
  22. * @param {string} hook name of hook
  23. */
  24. constructor(error, hook) {
  25. super(error ? error.message : undefined, error ? { cause: error } : {});
  26. /** @type {string} */
  27. this.hook = hook;
  28. /** @type {Error} */
  29. this.error = error;
  30. /** @type {string} */
  31. this.name = "HookWebpackError";
  32. /** @type {boolean} */
  33. this.hideStack = true;
  34. this.stack += `\n-- inner error --\n${error ? error.stack : ""}`;
  35. /** @type {string} */
  36. this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`;
  37. }
  38. /**
  39. * Serializes this instance into the provided serializer context.
  40. * @param {ObjectSerializerContext} context context
  41. */
  42. serialize(context) {
  43. context.write(this.error).write(this.hook);
  44. super.serialize(context);
  45. }
  46. /**
  47. * Restores this instance from the provided deserializer context.
  48. * @param {ObjectDeserializerContext} context context
  49. */
  50. deserialize(context) {
  51. this.error = context.read();
  52. const c1 = context.rest;
  53. this.hook = c1.read();
  54. super.deserialize(c1.rest);
  55. }
  56. }
  57. makeSerializable(HookWebpackError, "webpack/lib/errors/HookWebpackError");
  58. /**
  59. * Creates webpack error.
  60. * @param {Error} error an error
  61. * @param {string} hook name of the hook
  62. * @returns {WebpackError} a webpack error
  63. */
  64. const makeWebpackError = (error, hook) => {
  65. if (error instanceof WebpackError) return error;
  66. return new HookWebpackError(error, hook);
  67. };
  68. HookWebpackError.makeWebpackError = makeWebpackError;
  69. /**
  70. * Creates webpack error callback.
  71. * @template T
  72. * @param {(err: Error | null, result?: T) => void} callback webpack error callback
  73. * @param {string} hook name of hook
  74. * @returns {Callback<T>} generic callback
  75. */
  76. const makeWebpackErrorCallback = (callback, hook) => (err, result) => {
  77. if (err) {
  78. if (err instanceof WebpackError) {
  79. callback(err);
  80. return;
  81. }
  82. callback(new HookWebpackError(err, hook));
  83. return;
  84. }
  85. callback(null, result);
  86. };
  87. HookWebpackError.makeWebpackErrorCallback = makeWebpackErrorCallback;
  88. /**
  89. * Try run or webpack error.
  90. * @template T
  91. * @param {() => T} fn function which will be wrapping in try catch
  92. * @param {string} hook name of hook
  93. * @returns {T} the result
  94. */
  95. const tryRunOrWebpackError = (fn, hook) => {
  96. /** @type {T} */
  97. let r;
  98. try {
  99. r = fn();
  100. } catch (err) {
  101. if (err instanceof WebpackError) {
  102. throw err;
  103. }
  104. throw new HookWebpackError(/** @type {Error} */ (err), hook);
  105. }
  106. return r;
  107. };
  108. HookWebpackError.tryRunOrWebpackError = tryRunOrWebpackError;
  109. module.exports = HookWebpackError;