mirror of https://github.com/webpack/webpack.git
refactor: logic
This commit is contained in:
parent
207f198913
commit
52e710737c
|
@ -21,6 +21,7 @@ class WebpackError extends Error {
|
|||
* @param {{ cause?: unknown }} options error options
|
||||
*/
|
||||
constructor(message, options = {}) {
|
||||
// @ts-expect-error ES2018 doesn't `Error.cause`, but it can be used by developers
|
||||
super(message, options);
|
||||
|
||||
/** @type {string=} */
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
/** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
||||
/** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
||||
|
||||
/** @typedef {Error & { cause: unknown, errors: EXPECTED_ANY[] }} AggregateError */
|
||||
|
||||
class AggregateErrorSerializer {
|
||||
/**
|
||||
* @param {AggregateError} obj error
|
||||
|
@ -25,6 +27,7 @@ class AggregateErrorSerializer {
|
|||
*/
|
||||
deserialize(context) {
|
||||
const errors = context.read();
|
||||
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
|
||||
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
|
||||
const err = new AggregateError(errors);
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
/** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
||||
/** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
||||
|
||||
/** @typedef {Error & { cause?: unknown }} ErrorWithCause */
|
||||
|
||||
class ErrorObjectSerializer {
|
||||
/**
|
||||
* @param {ErrorConstructor | EvalErrorConstructor | RangeErrorConstructor | ReferenceErrorConstructor | SyntaxErrorConstructor | TypeErrorConstructor} Type error type
|
||||
|
@ -22,7 +24,10 @@ class ErrorObjectSerializer {
|
|||
serialize(obj, context) {
|
||||
context.write(obj.message);
|
||||
context.write(obj.stack);
|
||||
context.write(obj.cause);
|
||||
context.write(
|
||||
/** @type {ErrorWithCause} */
|
||||
(obj).cause
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,7 +39,8 @@ class ErrorObjectSerializer {
|
|||
|
||||
err.message = context.read();
|
||||
err.stack = context.read();
|
||||
err.cause = context.read();
|
||||
/** @type {ErrorWithCause} */
|
||||
(err).cause = context.read();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
|
|
@ -156,9 +156,11 @@ jsTypes.set(ReferenceError, new ErrorObjectSerializer(ReferenceError));
|
|||
jsTypes.set(SyntaxError, new ErrorObjectSerializer(SyntaxError));
|
||||
jsTypes.set(TypeError, new ErrorObjectSerializer(TypeError));
|
||||
|
||||
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
|
||||
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
|
||||
if (typeof AggregateError !== "undefined") {
|
||||
jsTypes.set(
|
||||
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
|
||||
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
|
||||
AggregateError,
|
||||
new AggregateErrorSerializer()
|
||||
|
|
|
@ -408,7 +408,10 @@ const countWithChildren = (compilation, getItems) => {
|
|||
return count;
|
||||
};
|
||||
|
||||
/** @type {ExtractorsByOption<string | Error | AggregateError | WebpackError, StatsError>} */
|
||||
/** @typedef {Error & { cause?: unknown }} ErrorWithCause */
|
||||
/** @typedef {Error & { errors: EXPECTED_ANY[] }} AggregateError */
|
||||
|
||||
/** @type {ExtractorsByOption<string | ErrorWithCause | AggregateError | WebpackError, StatsError>} */
|
||||
const EXTRACT_ERROR = {
|
||||
_: (object, error, context, { requestShortener }) => {
|
||||
// TODO webpack 6 disallow strings in the errors/warnings list
|
||||
|
@ -509,12 +512,16 @@ const EXTRACT_ERROR = {
|
|||
}
|
||||
},
|
||||
errorCause: (object, error, context, options, factory) => {
|
||||
if (typeof error !== "string" && error.cause) {
|
||||
if (
|
||||
typeof error !== "string" &&
|
||||
/** @type {ErrorWithCause} */ (error).cause
|
||||
) {
|
||||
const rawCause = /** @type {ErrorWithCause} */ (error).cause;
|
||||
/** @type {Error} */
|
||||
const cause =
|
||||
typeof error.cause === "string"
|
||||
? /** @type {Error} */ ({ message: error.cause })
|
||||
: /** @type {Error} */ (error.cause);
|
||||
typeof rawCause === "string"
|
||||
? /** @type {Error} */ ({ message: rawCause })
|
||||
: /** @type {Error} */ (rawCause);
|
||||
const { type } = context;
|
||||
|
||||
object.cause = factory.create(`${type}.cause`, cause, context);
|
||||
|
@ -527,7 +534,6 @@ const EXTRACT_ERROR = {
|
|||
(error).errors
|
||||
) {
|
||||
const { type } = context;
|
||||
|
||||
object.errors = factory.create(
|
||||
`${type}.errors`,
|
||||
/** @type {Error[]} */
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = function supportsAggregateError() {
|
||||
return typeof AggregateError !== "undefined";
|
||||
};
|
|
@ -0,0 +1,6 @@
|
|||
module.exports = function supportsErrorCause() {
|
||||
return (
|
||||
typeof new Error("test", { cause: new Error("cause") }).cause !==
|
||||
"undefined"
|
||||
);
|
||||
};
|
|
@ -0,0 +1,6 @@
|
|||
const supportsErrorCause = require("../../helpers/supportsErrorCause");
|
||||
const supportsAggregateError = require("../../helpers/supportsAggregateError");
|
||||
|
||||
module.exports = function () {
|
||||
return supportsErrorCause() && supportsAggregateError();
|
||||
};
|
|
@ -8,29 +8,37 @@ module.exports = {
|
|||
plugins: [
|
||||
compiler => {
|
||||
compiler.hooks.compilation.tap("Test", compilation => {
|
||||
// @ts-expect-error for tests
|
||||
const errCauseErr = new Error("error with case", {
|
||||
cause: new Error("error case")
|
||||
});
|
||||
compilation.errors.push(errCauseErr);
|
||||
compilation.warnings.push(errCauseErr);
|
||||
// @ts-expect-error for tests
|
||||
const errCauseErrCauseErr = new Error("error with nested error case", {
|
||||
// @ts-expect-error for tests
|
||||
cause: new Error("test", { cause: new Error("nested case") })
|
||||
});
|
||||
compilation.errors.push(errCauseErrCauseErr);
|
||||
compilation.warnings.push(errCauseErrCauseErr);
|
||||
// @ts-expect-error for tests
|
||||
const errCauseStr = new Error("error with string case", {
|
||||
cause: "string case"
|
||||
});
|
||||
compilation.errors.push(errCauseStr);
|
||||
compilation.warnings.push(errCauseStr);
|
||||
// @ts-expect-error for tests
|
||||
const aggregateError = new AggregateError(
|
||||
[
|
||||
// @ts-expect-error for tests
|
||||
new Error("first error", {
|
||||
// @ts-expect-error for tests
|
||||
cause: new Error("cause", {
|
||||
cause: new Error("nested cause in errors")
|
||||
})
|
||||
}),
|
||||
"second string error",
|
||||
// @ts-expect-error for tests
|
||||
new AggregateError(
|
||||
[new Error("nested first"), new Error("nested second")],
|
||||
"third nested aggregate error"
|
||||
|
@ -38,6 +46,7 @@ module.exports = {
|
|||
],
|
||||
"aggregate error",
|
||||
{
|
||||
// @ts-expect-error for tests
|
||||
cause: new Error("cause\ncause\ncause", {
|
||||
cause: "nested string cause"
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"module": "commonjs",
|
||||
"lib": ["es2017", "dom", "es2022.error"],
|
||||
"lib": ["es2017", "dom"],
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"module": "commonjs",
|
||||
"lib": ["es2017", "dom", "es2022.error"],
|
||||
"lib": ["es2017", "dom"],
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"noEmit": true,
|
||||
|
|
Loading…
Reference in New Issue