[6a3a178] | 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 WebpackError = require("./WebpackError");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./Module")} Module */
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * @template T
|
---|
| 14 | * @callback Callback
|
---|
| 15 | * @param {Error=} err
|
---|
| 16 | * @param {T=} stats
|
---|
| 17 | * @returns {void}
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | class HookWebpackError extends WebpackError {
|
---|
| 21 | /**
|
---|
| 22 | * Creates an instance of HookWebpackError.
|
---|
| 23 | * @param {Error} error inner error
|
---|
| 24 | * @param {string} hook name of hook
|
---|
| 25 | */
|
---|
| 26 | constructor(error, hook) {
|
---|
| 27 | super(error.message);
|
---|
| 28 |
|
---|
| 29 | this.name = "HookWebpackError";
|
---|
| 30 | this.hook = hook;
|
---|
| 31 | this.error = error;
|
---|
| 32 | this.hideStack = true;
|
---|
| 33 | this.details = `caused by plugins in ${hook}\n${error.stack}`;
|
---|
| 34 |
|
---|
| 35 | this.stack += `\n-- inner error --\n${error.stack}`;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | module.exports = HookWebpackError;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param {Error} error an error
|
---|
| 43 | * @param {string} hook name of the hook
|
---|
| 44 | * @returns {WebpackError} a webpack error
|
---|
| 45 | */
|
---|
| 46 | const makeWebpackError = (error, hook) => {
|
---|
| 47 | if (error instanceof WebpackError) return error;
|
---|
| 48 | return new HookWebpackError(error, hook);
|
---|
| 49 | };
|
---|
| 50 | module.exports.makeWebpackError = makeWebpackError;
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * @template T
|
---|
| 54 | * @param {function(WebpackError=, T=): void} callback webpack error callback
|
---|
| 55 | * @param {string} hook name of hook
|
---|
| 56 | * @returns {Callback<T>} generic callback
|
---|
| 57 | */
|
---|
| 58 | const makeWebpackErrorCallback = (callback, hook) => {
|
---|
| 59 | return (err, result) => {
|
---|
| 60 | if (err) {
|
---|
| 61 | if (err instanceof WebpackError) {
|
---|
| 62 | callback(err);
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 | callback(new HookWebpackError(err, hook));
|
---|
| 66 | return;
|
---|
| 67 | }
|
---|
| 68 | callback(null, result);
|
---|
| 69 | };
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | module.exports.makeWebpackErrorCallback = makeWebpackErrorCallback;
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @template T
|
---|
| 76 | * @param {function(): T} fn function which will be wrapping in try catch
|
---|
| 77 | * @param {string} hook name of hook
|
---|
| 78 | * @returns {T} the result
|
---|
| 79 | */
|
---|
| 80 | const tryRunOrWebpackError = (fn, hook) => {
|
---|
| 81 | let r;
|
---|
| 82 | try {
|
---|
| 83 | r = fn();
|
---|
| 84 | } catch (err) {
|
---|
| 85 | if (err instanceof WebpackError) {
|
---|
| 86 | throw err;
|
---|
| 87 | }
|
---|
| 88 | throw new HookWebpackError(err, hook);
|
---|
| 89 | }
|
---|
| 90 | return r;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | module.exports.tryRunOrWebpackError = tryRunOrWebpackError;
|
---|