source: trip-planner-front/node_modules/webpack/lib/HookWebpackError.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sean Larkin @thelarkinn
4*/
5
6"use strict";
7
8const 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
20class 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
39module.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 */
46const makeWebpackError = (error, hook) => {
47 if (error instanceof WebpackError) return error;
48 return new HookWebpackError(error, hook);
49};
50module.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 */
58const 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
72module.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 */
80const 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
93module.exports.tryRunOrWebpackError = tryRunOrWebpackError;
Note: See TracBrowser for help on using the repository browser.