Skip to content

Commit 6c1a40e

Browse files
authored
feat: should retain source error name (#11762)
1 parent 4a49fc0 commit 6c1a40e

File tree

7 files changed

+61
-35
lines changed

7 files changed

+61
-35
lines changed

crates/rspack_binding_api/src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,17 @@ impl std::fmt::Display for RspackError {
272272

273273
impl std::error::Error for RspackError {}
274274

275+
impl From<RspackError> for rspack_error::Error {
276+
fn from(value: RspackError) -> Error {
277+
let mut error = rspack_error::error!(format!("{}", value));
278+
error.code = Some(value.name);
279+
error.details = value.details;
280+
error.stack = value.stack;
281+
error.hide_stack = value.hide_stack;
282+
error
283+
}
284+
}
285+
275286
impl From<&Error> for RspackError {
276287
fn from(value: &Error) -> Self {
277288
let mut name = "Error".to_string();

crates/rspack_binding_api/src/plugins/js_loader/scheduler.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use napi::Either;
22
use rspack_core::{
33
AdditionalData, BUILTIN_LOADER_PREFIX, LoaderContext, NormalModuleLoaderShouldYield,
4-
NormalModuleLoaderStartYielding, RunnerContext, diagnostics::CapturedLoaderError,
4+
NormalModuleLoaderStartYielding, RunnerContext,
55
};
66
use rspack_error::{Result, ToStringResultToRspackResultExt};
77
use rspack_hook::plugin_hook;
@@ -110,14 +110,7 @@ pub(crate) fn merge_loader_context(
110110
} else {
111111
None
112112
};
113-
114-
return Err(
115-
CapturedLoaderError::new(
116-
Box::new(error.with_parent_error_name("ModuleBuildError")),
117-
details,
118-
)
119-
.into(),
120-
);
113+
return Err(error.with_parent_error_name("ModuleBuildError").into());
121114
}
122115

123116
let content = match from.content {

crates/rspack_core/src/diagnostics.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ impl ModuleBuildError {
4141
}
4242

4343
impl From<ModuleBuildError> for Error {
44-
fn from(mut value: ModuleBuildError) -> Error {
44+
fn from(value: ModuleBuildError) -> Error {
45+
let source = value.0;
46+
4547
let mut err = Error::error("Module build failed:".into());
46-
err.details = std::mem::take(&mut value.0.details);
47-
err.severity = value.0.severity;
48-
err.source_error = Some(Box::new(value.0));
48+
let details = source
49+
.hide_stack
50+
.unwrap_or(false)
51+
.then_some(source.stack.as_ref().map(|stack| stack.to_string()))
52+
.flatten();
53+
err.details = details;
54+
err.severity = source.severity;
55+
err.source_error = Some(Box::new(source));
4956
err.code = Some("ModuleBuildError".into());
5057
err
5158
}
@@ -110,28 +117,6 @@ impl ModuleParseError {
110117
}
111118
}
112119

113-
#[derive(Debug)]
114-
pub struct CapturedLoaderError {
115-
pub source: Box<dyn std::error::Error>,
116-
pub details: Option<String>,
117-
}
118-
119-
impl CapturedLoaderError {
120-
#[allow(clippy::too_many_arguments)]
121-
pub fn new(source: Box<dyn std::error::Error>, details: Option<String>) -> Self {
122-
Self { source, details }
123-
}
124-
}
125-
126-
impl From<CapturedLoaderError> for Error {
127-
fn from(value: CapturedLoaderError) -> Error {
128-
let mut error = rspack_error::error!(value.source.to_string());
129-
error.code = Some("CapturedLoaderError".into());
130-
error.details = value.details;
131-
error
132-
}
133-
}
134-
135120
/// Mark boxed errors as [crate::diagnostics::ModuleParseError],
136121
/// then, map it to diagnostics
137122
pub fn map_box_diagnostics_to_module_parse_diagnostics(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [/Module build failed/];

tests/rspack-test/configCases/loader/module-build-error-structure/index.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function () {
2+
const error = new Error("Cannot be used within pages/_document.js");
3+
error.name = "NextFontError";
4+
throw error;
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
entry: "./index.js",
4+
module: {
5+
rules: [
6+
{
7+
test: /\.js$/i,
8+
use: [{ loader: "./loader" }],
9+
}
10+
]
11+
},
12+
plugins: [
13+
{
14+
apply(compiler) {
15+
compiler.hooks.done.tap("PLUGIN", stats => {
16+
const { errors } = stats.compilation;
17+
expect(errors).toHaveLength(1);
18+
19+
const error = errors[0];
20+
expect(error).toMatchObject({
21+
name: "ModuleBuildError",
22+
error: {
23+
name: "NextFontError"
24+
}
25+
});
26+
expect(error.error.message).toContain("Cannot be used within pages/_document.js");
27+
});
28+
}
29+
}
30+
]
31+
};

0 commit comments

Comments
 (0)