webpack/lib/WebpackError.js

79 lines
2.0 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Jarid Margolin @jaridmargolin
*/
2018-07-30 23:08:51 +08:00
"use strict";
const inspect = require("util").inspect.custom;
2018-10-25 16:47:52 +08:00
const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./Chunk")} Chunk */
2018-07-14 03:30:55 +08:00
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("./Module")} Module */
2023-04-12 02:57:43 +08:00
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
2018-07-14 03:30:55 +08:00
class WebpackError extends Error {
/**
* Creates an instance of WebpackError.
* @param {string=} message error message
* @param {{ cause?: unknown }} options error options
*/
constructor(message, options = {}) {
2025-04-23 06:27:18 +08:00
// @ts-expect-error ES2018 doesn't `Error.cause`, but it can be used by developers
super(message, options);
2018-04-08 08:44:27 +08:00
2024-03-14 23:15:13 +08:00
/** @type {string=} */
2018-04-12 00:44:28 +08:00
this.details = undefined;
2024-03-14 23:15:13 +08:00
/** @type {(Module | null)=} */
this.module = undefined;
2024-03-14 23:15:13 +08:00
/** @type {DependencyLocation=} */
2018-07-14 03:30:55 +08:00
this.loc = undefined;
2024-03-14 23:15:13 +08:00
/** @type {boolean=} */
2018-10-25 16:49:51 +08:00
this.hideStack = undefined;
2024-03-14 23:15:13 +08:00
/** @type {Chunk=} */
this.chunk = undefined;
2024-03-14 23:15:13 +08:00
/** @type {string=} */
this.file = undefined;
}
[inspect]() {
return (
this.stack +
(this.details ? `\n${this.details}` : "") +
(this.cause ? `\n${this.cause}` : "")
);
}
2018-10-25 16:47:52 +08:00
2023-04-12 02:57:43 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2018-10-25 16:49:51 +08:00
serialize({ write }) {
write(this.name);
write(this.message);
write(this.stack);
write(this.cause);
2018-10-25 16:47:52 +08:00
write(this.details);
write(this.loc);
2018-10-25 16:49:51 +08:00
write(this.hideStack);
2018-10-25 16:47:52 +08:00
}
2023-04-12 02:57:43 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2018-10-25 16:49:51 +08:00
deserialize({ read }) {
this.name = read();
this.message = read();
this.stack = read();
this.cause = read();
2018-10-25 16:47:52 +08:00
this.details = read();
this.loc = read();
2018-10-25 16:49:51 +08:00
this.hideStack = read();
2018-10-25 16:47:52 +08:00
}
}
2018-10-25 16:47:52 +08:00
makeSerializable(WebpackError, "webpack/lib/WebpackError");
module.exports = WebpackError;