| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Sean Larkin @thelarkinn
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const WebpackError = require("./WebpackError");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 12 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Defines the callback callback.
|
|---|
| 16 | * @template T
|
|---|
| 17 | * @callback Callback
|
|---|
| 18 | * @param {Error | null} err
|
|---|
| 19 | * @param {T=} stats
|
|---|
| 20 | * @returns {void}
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | class HookWebpackError extends WebpackError {
|
|---|
| 24 | /**
|
|---|
| 25 | * Creates an instance of HookWebpackError.
|
|---|
| 26 | * @param {Error} error inner error
|
|---|
| 27 | * @param {string} hook name of hook
|
|---|
| 28 | */
|
|---|
| 29 | constructor(error, hook) {
|
|---|
| 30 | super(error ? error.message : undefined, error ? { cause: error } : {});
|
|---|
| 31 |
|
|---|
| 32 | this.hook = hook;
|
|---|
| 33 | this.error = error;
|
|---|
| 34 | /** @type {string} */
|
|---|
| 35 | this.name = "HookWebpackError";
|
|---|
| 36 | this.hideStack = true;
|
|---|
| 37 | this.stack += `\n-- inner error --\n${error ? error.stack : ""}`;
|
|---|
| 38 | this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Serializes this instance into the provided serializer context.
|
|---|
| 43 | * @param {ObjectSerializerContext} context context
|
|---|
| 44 | */
|
|---|
| 45 | serialize(context) {
|
|---|
| 46 | const { write } = context;
|
|---|
| 47 |
|
|---|
| 48 | write(this.error);
|
|---|
| 49 | write(this.hook);
|
|---|
| 50 |
|
|---|
| 51 | super.serialize(context);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Restores this instance from the provided deserializer context.
|
|---|
| 56 | * @param {ObjectDeserializerContext} context context
|
|---|
| 57 | */
|
|---|
| 58 | deserialize(context) {
|
|---|
| 59 | const { read } = context;
|
|---|
| 60 |
|
|---|
| 61 | this.error = read();
|
|---|
| 62 | this.hook = read();
|
|---|
| 63 |
|
|---|
| 64 | super.deserialize(context);
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | makeSerializable(HookWebpackError, "webpack/lib/errors/HookWebpackError");
|
|---|
| 69 |
|
|---|
| 70 | module.exports = HookWebpackError;
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Creates webpack error.
|
|---|
| 74 | * @param {Error} error an error
|
|---|
| 75 | * @param {string} hook name of the hook
|
|---|
| 76 | * @returns {WebpackError} a webpack error
|
|---|
| 77 | */
|
|---|
| 78 | const makeWebpackError = (error, hook) => {
|
|---|
| 79 | if (error instanceof WebpackError) return error;
|
|---|
| 80 | return new HookWebpackError(error, hook);
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | module.exports.makeWebpackError = makeWebpackError;
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Creates webpack error callback.
|
|---|
| 87 | * @template T
|
|---|
| 88 | * @param {(err: Error | null, result?: T) => void} callback webpack error callback
|
|---|
| 89 | * @param {string} hook name of hook
|
|---|
| 90 | * @returns {Callback<T>} generic callback
|
|---|
| 91 | */
|
|---|
| 92 | const makeWebpackErrorCallback = (callback, hook) => (err, result) => {
|
|---|
| 93 | if (err) {
|
|---|
| 94 | if (err instanceof WebpackError) {
|
|---|
| 95 | callback(err);
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 | callback(new HookWebpackError(err, hook));
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | callback(null, result);
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | module.exports.makeWebpackErrorCallback = makeWebpackErrorCallback;
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Try run or webpack error.
|
|---|
| 108 | * @template T
|
|---|
| 109 | * @param {() => T} fn function which will be wrapping in try catch
|
|---|
| 110 | * @param {string} hook name of hook
|
|---|
| 111 | * @returns {T} the result
|
|---|
| 112 | */
|
|---|
| 113 | const tryRunOrWebpackError = (fn, hook) => {
|
|---|
| 114 | /** @type {T} */
|
|---|
| 115 | let r;
|
|---|
| 116 | try {
|
|---|
| 117 | r = fn();
|
|---|
| 118 | } catch (err) {
|
|---|
| 119 | if (err instanceof WebpackError) {
|
|---|
| 120 | throw err;
|
|---|
| 121 | }
|
|---|
| 122 | throw new HookWebpackError(/** @type {Error} */ (err), hook);
|
|---|
| 123 | }
|
|---|
| 124 | return r;
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | module.exports.tryRunOrWebpackError = tryRunOrWebpackError;
|
|---|