Skip to content

Commit 4a8cfd3

Browse files
fix: normalize a non-Error thrown from a runtime module's generate()
`toError` is what the rest of the compilation normalizes a thrown value with, so a `generate()` throwing `null` reports the runtime module rather than a TypeError raised while building the message. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01EAcvnxxuoLV22VqcY51yCg
1 parent 64d800f commit 4a8cfd3

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎lib/runtime/RuntimeModule.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ class RuntimeModule extends Module {
148148
this.generate()
149149
: this.getGeneratedCode();
150150
} catch (err) {
151+
const { toError } = require("../errors/ErrorHelpers");
151152
const RuntimeModuleGenerationError = require("../errors/RuntimeModuleGenerationError");
152153

153-
throw new RuntimeModuleGenerationError(this, /** @type {Error} */ (err));
154+
throw new RuntimeModuleGenerationError(this, toError(err));
154155
}
155156
if (code !== null && code !== undefined) {
156157
hash.update(code);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
/Generating the runtime module webpack\/runtime\/throwing non error \(ThrowingNonErrorRuntimeModule\) for hashing failed:/,
6+
/generate of the throwing runtime module failed/
7+
],
8+
[/generate of the throwing runtime module failed/]
9+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should name the runtime module that threw a non-Error value", () => {
2+
expect(1).toBe(1);
3+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const { RuntimeModule } = require("../../../../");
4+
5+
const PLUGIN_NAME = "ThrowingNonErrorRuntimeModulePlugin";
6+
7+
class ThrowingNonErrorRuntimeModule extends RuntimeModule {
8+
constructor() {
9+
super("throwing non error");
10+
}
11+
12+
/**
13+
* Generates runtime code for this runtime module.
14+
* @returns {string} runtime code
15+
*/
16+
generate() {
17+
// eslint-disable-next-line no-throw-literal
18+
throw "generate of the throwing runtime module failed";
19+
}
20+
}
21+
22+
/** @type {import("../../../../").Configuration} */
23+
module.exports = {
24+
plugins: [
25+
{
26+
apply(compiler) {
27+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
28+
compilation.hooks.additionalTreeRuntimeRequirements.tap(
29+
PLUGIN_NAME,
30+
(chunk) => {
31+
compilation.addRuntimeModule(
32+
chunk,
33+
new ThrowingNonErrorRuntimeModule()
34+
);
35+
}
36+
);
37+
});
38+
}
39+
}
40+
]
41+
};

0 commit comments

Comments
 (0)