ModuleBuildError.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { cutOffLoaderExecution } = require("./ErrorHelpers");
  7. const WebpackError = require("./WebpackError");
  8. const makeSerializable = require("./util/makeSerializable");
  9. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  10. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  11. class ModuleBuildError extends WebpackError {
  12. /**
  13. * @param {string | Error&any} err error thrown
  14. * @param {{from?: string|null}} info additional info
  15. */
  16. constructor(err, { from = null } = {}) {
  17. let message = "Module build failed";
  18. let details = undefined;
  19. if (from) {
  20. message += ` (from ${from}):\n`;
  21. } else {
  22. message += ": ";
  23. }
  24. if (err !== null && typeof err === "object") {
  25. if (typeof err.stack === "string" && err.stack) {
  26. const stack = cutOffLoaderExecution(err.stack);
  27. if (!err.hideStack) {
  28. message += stack;
  29. } else {
  30. details = stack;
  31. if (typeof err.message === "string" && err.message) {
  32. message += err.message;
  33. } else {
  34. message += err;
  35. }
  36. }
  37. } else if (typeof err.message === "string" && err.message) {
  38. message += err.message;
  39. } else {
  40. message += String(err);
  41. }
  42. } else {
  43. message += String(err);
  44. }
  45. super(message);
  46. this.name = "ModuleBuildError";
  47. this.details = details;
  48. this.error = err;
  49. }
  50. /**
  51. * @param {ObjectSerializerContext} context context
  52. */
  53. serialize(context) {
  54. const { write } = context;
  55. write(this.error);
  56. super.serialize(context);
  57. }
  58. /**
  59. * @param {ObjectDeserializerContext} context context
  60. */
  61. deserialize(context) {
  62. const { read } = context;
  63. this.error = read();
  64. super.deserialize(context);
  65. }
  66. }
  67. makeSerializable(ModuleBuildError, "webpack/lib/ModuleBuildError");
  68. module.exports = ModuleBuildError;