HookWebpackError.js 3.1 KB

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