source: imaps-frontend/node_modules/webpack/lib/HookWebpackError.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • 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 | null} 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 | null, T=): void} callback webpack error callback
55 * @param {string} hook name of hook
56 * @returns {Callback<T>} generic callback
57 */
58const makeWebpackErrorCallback = (callback, hook) => (err, result) => {
59 if (err) {
60 if (err instanceof WebpackError) {
61 callback(err);
62 return;
63 }
64 callback(new HookWebpackError(err, hook));
65 return;
66 }
67 callback(null, result);
68};
69
70module.exports.makeWebpackErrorCallback = makeWebpackErrorCallback;
71
72/**
73 * @template T
74 * @param {function(): T} fn function which will be wrapping in try catch
75 * @param {string} hook name of hook
76 * @returns {T} the result
77 */
78const tryRunOrWebpackError = (fn, hook) => {
79 let r;
80 try {
81 r = fn();
82 } catch (err) {
83 if (err instanceof WebpackError) {
84 throw err;
85 }
86 throw new HookWebpackError(/** @type {Error} */ (err), hook);
87 }
88 return r;
89};
90
91module.exports.tryRunOrWebpackError = tryRunOrWebpackError;
Note: See TracBrowser for help on using the repository browser.