HookWebpackError.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @template T
  12. * @callback Callback
  13. * @param {Error | null} err
  14. * @param {T=} stats
  15. * @returns {void}
  16. */
  17. class HookWebpackError extends WebpackError {
  18. /**
  19. * Creates an instance of HookWebpackError.
  20. * @param {Error} error inner error
  21. * @param {string} hook name of hook
  22. */
  23. constructor(error, hook) {
  24. super(error ? error.message : undefined, error ? { cause: error } : {});
  25. this.hook = hook;
  26. this.error = error;
  27. /** @type {string} */
  28. this.name = "HookWebpackError";
  29. this.hideStack = true;
  30. this.stack += `\n-- inner error --\n${error ? error.stack : ""}`;
  31. this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`;
  32. }
  33. /**
  34. * @param {ObjectSerializerContext} context context
  35. */
  36. serialize(context) {
  37. const { write } = context;
  38. write(this.error);
  39. write(this.hook);
  40. super.serialize(context);
  41. }
  42. /**
  43. * @param {ObjectDeserializerContext} context context
  44. */
  45. deserialize(context) {
  46. const { read } = context;
  47. this.error = read();
  48. this.hook = read();
  49. super.deserialize(context);
  50. }
  51. }
  52. makeSerializable(HookWebpackError, "webpack/lib/HookWebpackError");
  53. module.exports = HookWebpackError;
  54. /**
  55. * @param {Error} error an error
  56. * @param {string} hook name of the hook
  57. * @returns {WebpackError} a webpack error
  58. */
  59. const makeWebpackError = (error, hook) => {
  60. if (error instanceof WebpackError) return error;
  61. return new HookWebpackError(error, hook);
  62. };
  63. module.exports.makeWebpackError = makeWebpackError;
  64. /**
  65. * @template T
  66. * @param {(err: WebpackError | null, result?: T) => void} callback webpack error callback
  67. * @param {string} hook name of hook
  68. * @returns {Callback<T>} generic callback
  69. */
  70. const makeWebpackErrorCallback = (callback, hook) => (err, result) => {
  71. if (err) {
  72. if (err instanceof WebpackError) {
  73. callback(err);
  74. return;
  75. }
  76. callback(new HookWebpackError(err, hook));
  77. return;
  78. }
  79. callback(null, result);
  80. };
  81. module.exports.makeWebpackErrorCallback = makeWebpackErrorCallback;
  82. /**
  83. * @template T
  84. * @param {() => T} fn function which will be wrapping in try catch
  85. * @param {string} hook name of hook
  86. * @returns {T} the result
  87. */
  88. const tryRunOrWebpackError = (fn, hook) => {
  89. /** @type {T} */
  90. let r;
  91. try {
  92. r = fn();
  93. } catch (err) {
  94. if (err instanceof WebpackError) {
  95. throw err;
  96. }
  97. throw new HookWebpackError(/** @type {Error} */ (err), hook);
  98. }
  99. return r;
  100. };
  101. module.exports.tryRunOrWebpackError = tryRunOrWebpackError;