/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/errors/HookWebpackError.js
125 строк
3 KB
Alexander Akait
refactor: annotate constructor fields with @type to improve type coverage (#21265)
24 июн 2026, 13:36
Не верифицирован
24 июн 2026, 13:36
e8f9334
Код
Авторство
О чём код?
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Sean Larkin @thelarkinn */ "use strict"; const makeSerializable = require("../util/makeSerializable"); const WebpackError = require("./WebpackError"); /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Error, string]>} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Error, string]>} ObjectSerializerContext */ /** * Defines the callback callback. * @template T * @callback Callback * @param {Error | null} err * @param {T=} stats * @returns {void} */ class HookWebpackError extends WebpackError { /** * Creates an instance of HookWebpackError. * @param {Error} error inner error * @param {string} hook name of hook */ constructor(error, hook) { super(error ? error.message : undefined, error ? { cause: error } : {}); /** @type {string} */ this.hook = hook; /** @type {Error} */ this.error = error; /** @type {string} */ this.name = "HookWebpackError"; /** @type {boolean} */ this.hideStack = true; this.stack += `\n-- inner error --\n${error ? error.stack : ""}`; /** @type {string} */ this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`; } /** * Serializes this instance into the provided serializer context. * @param {ObjectSerializerContext} context context */ serialize(context) { context.write(this.error).write(this.hook); super.serialize(context); } /** * Restores this instance from the provided deserializer context. * @param {ObjectDeserializerContext} context context */ deserialize(context) { this.error = context.read(); const c1 = context.rest; this.hook = c1.read(); super.deserialize(c1.rest); } } makeSerializable(HookWebpackError, "webpack/lib/errors/HookWebpackError"); module.exports = HookWebpackError; /** * Creates webpack error. * @param {Error} error an error * @param {string} hook name of the hook * @returns {WebpackError} a webpack error */ const makeWebpackError = (error, hook) => { if (error instanceof WebpackError) return error; return new HookWebpackError(error, hook); }; module.exports.makeWebpackError = makeWebpackError; /** * Creates webpack error callback. * @template T * @param {(err: Error | null, result?: T) => void} callback webpack error callback * @param {string} hook name of hook * @returns {Callback<T>} generic callback */ const makeWebpackErrorCallback = (callback, hook) => (err, result) => { if (err) { if (err instanceof WebpackError) { callback(err); return; } callback(new HookWebpackError(err, hook)); return; } callback(null, result); }; module.exports.makeWebpackErrorCallback = makeWebpackErrorCallback; /** * Try run or webpack error. * @template T * @param {() => T} fn function which will be wrapping in try catch * @param {string} hook name of hook * @returns {T} the result */ const tryRunOrWebpackError = (fn, hook) => { /** @type {T} */ let r; try { r = fn(); } catch (err) { if (err instanceof WebpackError) { throw err; } throw new HookWebpackError(/** @type {Error} */ (err), hook); } return r; }; module.exports.tryRunOrWebpackError = tryRunOrWebpackError;