Skip to content

Commit 949890a

Browse files
authored
Merge pull request webpack#7472 from webpack/bugfix/error-origin-loc
Cleanup error location and origin information
2 parents e9195c4 + 53103a9 commit 949890a

28 files changed

+191
-169
lines changed

lib/AsyncDependencyToInitialChunkError.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class AsyncDependencyToInitialChunkError extends WebpackError {
1616
* @param {TODO} loc location of dependency
1717
*/
1818
constructor(chunkName, module, loc) {
19-
super();
19+
super(
20+
`It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`
21+
);
2022

2123
this.name = "AsyncDependencyToInitialChunkError";
22-
this.message = `It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`;
2324
this.module = module;
24-
this.origin = module;
25-
this.originLoc = loc;
25+
this.loc = loc;
2626

2727
Error.captureStackTrace(this, this.constructor);
2828
}

lib/CaseSensitiveModulesWarning.js

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,60 @@ const WebpackError = require("./WebpackError");
88

99
/** @typedef {import("./Module")} Module */
1010

11+
/**
12+
* @param {Module[]} modules the modules to be sorted
13+
* @returns {Module[]} sorted version of original modules
14+
*/
15+
const sortModules = modules => {
16+
return modules.slice().sort((a, b) => {
17+
a = a.identifier();
18+
b = b.identifier();
19+
/* istanbul ignore next */
20+
if (a < b) return -1;
21+
/* istanbul ignore next */
22+
if (a > b) return 1;
23+
/* istanbul ignore next */
24+
return 0;
25+
});
26+
};
27+
28+
/**
29+
* @param {Module[]} modules each module from throw
30+
* @returns {string} each message from provided moduels
31+
*/
32+
const createModulesListMessage = modules => {
33+
return modules
34+
.map(m => {
35+
let message = `* ${m.identifier()}`;
36+
const validReasons = m.reasons.filter(reason => reason.module);
37+
38+
if (validReasons.length > 0) {
39+
message += `\n Used by ${validReasons.length} module(s), i. e.`;
40+
message += `\n ${validReasons[0].module.identifier()}`;
41+
}
42+
return message;
43+
})
44+
.join("\n");
45+
};
46+
1147
class CaseSensitiveModulesWarning extends WebpackError {
1248
/**
1349
* Creates an instance of CaseSensitiveModulesWarning.
1450
* @param {Module[]} modules modules that were detected
1551
*/
1652
constructor(modules) {
17-
super();
18-
19-
this.name = "CaseSensitiveModulesWarning";
20-
const sortedModules = this._sort(modules);
21-
const modulesList = this._moduleMessages(sortedModules);
22-
this.message = `There are multiple modules with names that only differ in casing.
53+
const sortedModules = sortModules(modules);
54+
const modulesList = createModulesListMessage(sortedModules);
55+
super(`There are multiple modules with names that only differ in casing.
2356
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
2457
Use equal casing. Compare these module identifiers:
25-
${modulesList}`;
58+
${modulesList}`);
2659

60+
this.name = "CaseSensitiveModulesWarning";
2761
this.origin = this.module = sortedModules[0];
2862

2963
Error.captureStackTrace(this, this.constructor);
3064
}
31-
32-
/**
33-
* @private
34-
* @param {Module[]} modules the modules to be sorted
35-
* @returns {Module[]} sorted version of original modules
36-
*/
37-
_sort(modules) {
38-
return modules.slice().sort((a, b) => {
39-
a = a.identifier();
40-
b = b.identifier();
41-
/* istanbul ignore next */
42-
if (a < b) return -1;
43-
/* istanbul ignore next */
44-
if (a > b) return 1;
45-
/* istanbul ignore next */
46-
return 0;
47-
});
48-
}
49-
50-
/**
51-
* @private
52-
* @param {Module[]} modules each module from throw
53-
* @returns {string} each message from provided moduels
54-
*/
55-
_moduleMessages(modules) {
56-
return modules
57-
.map(m => {
58-
let message = `* ${m.identifier()}`;
59-
const validReasons = m.reasons.filter(reason => reason.module);
60-
61-
if (validReasons.length > 0) {
62-
message += `\n Used by ${validReasons.length} module(s), i. e.`;
63-
message += `\n ${validReasons[0].module.identifier()}`;
64-
}
65-
return message;
66-
})
67-
.join("\n");
68-
}
6965
}
7066

7167
module.exports = CaseSensitiveModulesWarning;

lib/Compilation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ class Compilation extends Tapable {
487487

488488
const errorAndCallback = err => {
489489
err.origin = module;
490+
err.dependencies = dependencies;
490491
this.errors.push(err);
491492
if (bail) {
492493
callback(err);
@@ -531,7 +532,7 @@ class Compilation extends Tapable {
531532
if (err) {
532533
semaphore.release();
533534
return errorOrWarningAndCallback(
534-
new ModuleNotFoundError(module, err, dependencies)
535+
new ModuleNotFoundError(module, err)
535536
);
536537
}
537538
if (!dependentModule) {

lib/EntryModuleNotFoundError.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ const WebpackError = require("./WebpackError");
88

99
class EntryModuleNotFoundError extends WebpackError {
1010
constructor(err) {
11-
super();
11+
super("Entry module not found: " + err);
1212

1313
this.name = "EntryModuleNotFoundError";
14-
this.message = "Entry module not found: " + err;
1514
this.details = err.details;
1615
this.error = err;
1716

lib/HarmonyLinkingError.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ const WebpackError = require("./WebpackError");
88
module.exports = class HarmonyLinkingError extends WebpackError {
99
/** @param {string} message Error message */
1010
constructor(message) {
11-
super();
11+
super(message);
1212
this.name = "HarmonyLinkingError";
13-
this.message = message;
1413
this.hideStack = true;
1514

1615
Error.captureStackTrace(this, this.constructor);

lib/ModuleBuildError.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,32 @@ const { cutOffLoaderExecution } = require("./ErrorHelpers");
99

1010
class ModuleBuildError extends WebpackError {
1111
constructor(module, err) {
12-
super();
13-
14-
this.name = "ModuleBuildError";
15-
this.message = "Module build failed: ";
12+
let message = "Module build failed: ";
13+
let details = undefined;
1614
if (err !== null && typeof err === "object") {
1715
if (typeof err.stack === "string" && err.stack) {
1816
var stack = cutOffLoaderExecution(err.stack);
1917
if (!err.hideStack) {
20-
this.message += stack;
18+
message += stack;
2119
} else {
22-
this.details = stack;
20+
details = stack;
2321
if (typeof err.message === "string" && err.message) {
24-
this.message += err.message;
22+
message += err.message;
2523
} else {
26-
this.message += err;
24+
message += err;
2725
}
2826
}
2927
} else if (typeof err.message === "string" && err.message) {
30-
this.message += err.message;
28+
message += err.message;
3129
} else {
32-
this.message += err;
30+
message += err;
3331
}
3432
}
33+
34+
super(message);
35+
36+
this.name = "ModuleBuildError";
37+
this.details = details;
3538
this.module = module;
3639
this.error = err;
3740

lib/ModuleDependencyError.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"use strict";
66

77
const WebpackError = require("./WebpackError");
8-
const formatLocation = require("./formatLocation");
98

109
/** @typedef {import("./Module")} Module */
1110

@@ -17,15 +16,15 @@ class ModuleDependencyError extends WebpackError {
1716
* @param {TODO} loc location of dependency
1817
*/
1918
constructor(module, err, loc) {
20-
super();
19+
super(err.message);
2120

2221
this.name = "ModuleDependencyError";
23-
this.message = `${formatLocation(loc)} ${err.message}`;
2422
this.details = err.stack
2523
.split("\n")
2624
.slice(1)
2725
.join("\n");
28-
this.origin = this.module = module;
26+
this.module = module;
27+
this.loc = loc;
2928
this.error = err;
3029

3130
Error.captureStackTrace(this, this.constructor);

lib/ModuleDependencyWarning.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
"use strict";
66

77
const WebpackError = require("./WebpackError");
8-
const formatLocation = require("./formatLocation");
98

109
module.exports = class ModuleDependencyWarning extends WebpackError {
1110
constructor(module, err, loc) {
12-
super();
11+
super(err.message);
1312

1413
this.name = "ModuleDependencyWarning";
15-
this.message = `${formatLocation(loc)} ${err.message}`;
1614
this.details = err.stack
1715
.split("\n")
1816
.slice(1)
1917
.join("\n");
20-
this.origin = this.module = module;
18+
this.module = module;
19+
this.loc = loc;
2120
this.error = err;
2221

2322
Error.captureStackTrace(this, this.constructor);

lib/ModuleError.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ const { cleanUp } = require("./ErrorHelpers");
99

1010
class ModuleError extends WebpackError {
1111
constructor(module, err) {
12-
super();
12+
super(err && typeof err === "object" && err.message ? err.message : err);
1313

1414
this.name = "ModuleError";
1515
this.module = module;
16-
this.message =
17-
err && typeof err === "object" && err.message ? err.message : err;
1816
this.error = err;
1917
this.details =
2018
err && typeof err === "object" && err.stack

lib/ModuleNotFoundError.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
const WebpackError = require("./WebpackError");
88

99
class ModuleNotFoundError extends WebpackError {
10-
constructor(module, err, dependencies) {
11-
super();
10+
constructor(module, err) {
11+
super("Module not found: " + err);
1212

1313
this.name = "ModuleNotFoundError";
14-
this.message = "Module not found: " + err;
1514
this.details = err.details;
1615
this.missing = err.missing;
1716
this.module = module;
18-
this.origin = module;
19-
this.dependencies = dependencies;
2017
this.error = err;
2118

2219
Error.captureStackTrace(this, this.constructor);

lib/ModuleParseError.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66

77
const WebpackError = require("./WebpackError");
88

9+
/** @typedef {import("./Module")} Module */
10+
911
class ModuleParseError extends WebpackError {
12+
/**
13+
* @param {Module} module the errored module
14+
* @param {string} source source code
15+
* @param {Error&any} err the parse error
16+
*/
1017
constructor(module, source, err) {
11-
super();
12-
13-
this.name = "ModuleParseError";
14-
this.message = "Module parse failed: " + err.message;
15-
this.message +=
16-
"\nYou may need an appropriate loader to handle this file type.";
18+
let message = "Module parse failed: " + err.message;
19+
let loc = undefined;
20+
message += "\nYou may need an appropriate loader to handle this file type.";
1721
if (
1822
err.loc &&
1923
typeof err.loc === "object" &&
@@ -22,19 +26,28 @@ class ModuleParseError extends WebpackError {
2226
var lineNumber = err.loc.line;
2327
if (/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) {
2428
// binary file
25-
this.message += "\n(Source code omitted for this binary file)";
29+
message += "\n(Source code omitted for this binary file)";
2630
} else {
27-
source = source.split("\n");
28-
this.message +=
29-
"\n| " +
30-
source
31-
.slice(Math.max(0, lineNumber - 3), lineNumber + 2)
32-
.join("\n| ");
31+
const sourceLines = source.split("\n");
32+
const start = Math.max(0, lineNumber - 3);
33+
const linesBefore = sourceLines.slice(start, lineNumber - 1);
34+
const theLine = sourceLines[lineNumber - 1];
35+
const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
36+
message +=
37+
linesBefore.map(l => `\n| ${l}`).join("") +
38+
`\n> ${theLine}` +
39+
linesAfter.map(l => `\n| ${l}`).join("");
3340
}
41+
loc = err.loc;
3442
} else {
35-
this.message += "\n" + err.stack;
43+
message += "\n" + err.stack;
3644
}
45+
46+
super(message);
47+
48+
this.name = "ModuleParseError";
3749
this.module = module;
50+
this.loc = loc;
3851
this.error = err;
3952

4053
Error.captureStackTrace(this, this.constructor);

lib/ModuleWarning.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const { cleanUp } = require("./ErrorHelpers");
99

1010
class ModuleWarning extends WebpackError {
1111
constructor(module, warning) {
12-
super();
12+
super(
13+
warning && typeof warning === "object" && warning.message
14+
? warning.message
15+
: warning
16+
);
1317

1418
this.name = "ModuleWarning";
1519
this.module = module;
16-
this.message =
17-
warning && typeof warning === "object" && warning.message
18-
? warning.message
19-
: warning;
2020
this.warning = warning;
2121
this.details =
2222
warning && typeof warning === "object" && warning.stack

lib/ParserHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ParserHelpers.expressionIsUnsupported = (parser, message) => {
8888
parser.state.current.addDependency(dep);
8989
if (!parser.state.module) return;
9090
parser.state.module.warnings.push(
91-
new UnsupportedFeatureWarning(parser.state.module, message)
91+
new UnsupportedFeatureWarning(parser.state.module, message, expr.loc)
9292
);
9393
return true;
9494
};

lib/RemovedPluginError.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ const WebpackError = require("./WebpackError");
44

55
module.exports = class RemovedPluginError extends WebpackError {
66
constructor(message) {
7-
super();
8-
9-
this.message = message;
7+
super(message);
108

119
Error.captureStackTrace(this, this.constructor);
1210
}

0 commit comments

Comments
 (0)