HookWebpackError.js 2.8 KB

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