Skip to content

Commit 34c47a0

Browse files
committed
fix
1 parent 9e4525e commit 34c47a0

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

crates/node_binding/src/error.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use cow_utils::CowUtils;
12
use derive_more::Debug;
2-
use napi::{bindgen_prelude::ToNapiValue, JsValue};
3+
use napi::{bindgen_prelude::ToNapiValue, JsValue, ValueType};
34
use napi_derive::napi;
45
use rspack_core::ModuleIdentifier;
56
use rspack_error::{
@@ -124,7 +125,8 @@ impl napi::bindgen_prelude::FromNapiValue for RspackError {
124125
napi_val: napi::bindgen_prelude::sys::napi_value,
125126
) -> napi::bindgen_prelude::Result<RspackError> {
126127
let unknown = napi::bindgen_prelude::Unknown::from_napi_value(env, napi_val)?;
127-
if !unknown.is_error()? {
128+
let ty = unknown.get_type()?;
129+
if ty != ValueType::Object {
128130
let error = unknown.coerce_to_string()?.into_utf8()?.into_owned()?;
129131
return Ok(RspackError {
130132
name: "NonErrorEmittedError".to_string(),
@@ -141,30 +143,8 @@ impl napi::bindgen_prelude::FromNapiValue for RspackError {
141143
}
142144

143145
let obj = napi::bindgen_prelude::Object::from_napi_value(env, napi_val)?;
144-
let name: String = obj
145-
.get("name")
146-
.map_err(|mut err| {
147-
err.reason = format!("{} on {}.{}", err.reason, "RspackError", "name");
148-
err
149-
})?
150-
.ok_or_else(|| {
151-
napi::bindgen_prelude::Error::new(
152-
napi::bindgen_prelude::Status::InvalidArg,
153-
"Missing field `name`",
154-
)
155-
})?;
156-
let message: String = obj
157-
.get("message")
158-
.map_err(|mut err| {
159-
err.reason = format!("{} on {}.{}", err.reason, "RspackError", "message");
160-
err
161-
})?
162-
.ok_or_else(|| {
163-
napi::bindgen_prelude::Error::new(
164-
napi::bindgen_prelude::Status::InvalidArg,
165-
"Missing field `message`",
166-
)
167-
})?;
146+
let name: String = obj.get("name")?.unwrap_or("Error".to_string());
147+
let message: String = obj.get("message")?.unwrap_or("Error".to_string());
168148
let module = obj.get::<ModuleObject>("module").unwrap_or(None);
169149
let module_identifier = if let Some(module) = &module {
170150
Some(module.identifier())
@@ -230,7 +210,10 @@ impl RspackError {
230210
// the original error message is extracted from the `message`
231211
error = Some(Box::new(RspackError {
232212
name: "Error".to_string(),
233-
message: diagnostic.to_string().replace("Module not found: ", ""),
213+
message: diagnostic
214+
.to_string()
215+
.cow_replace("Module not found: ", "")
216+
.to_string(),
234217
severity: None,
235218
module_identifier: None,
236219
module: None,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/Module not found/],
3+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
entry: "./index",
4+
plugins: [
5+
{
6+
apply(compiler) {
7+
compiler.hooks.done.tap("TestPlugin", stats => {
8+
const erros = stats.compilation.errors;
9+
expect(erros.length).toBe(1);
10+
expect(erros[0].name).toBe("ModuleNotFoundError");
11+
expect(erros[0].message).toContain("Module not found: Can't resolve './index'");
12+
13+
expect(erros[0].error).toBeDefined();
14+
});
15+
}
16+
}
17+
]
18+
};

packages/rspack-test-tools/tests/configCases/errors/push-custom-error/rspack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
column: 0
2020
}
2121
};
22-
compilation.errors.push([error]);
22+
compilation.errors.push(error);
2323
});
2424
});
2525
}

tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ exports[`StatsTestCases should print correct stats for errors-space-error 1`] =
511511
512512
ERROR in ./index.js (./loader!./index.js)
513513
× stack1stack2
514-
stack3
514+
stack3loader error1 (from: Xdir/errors-xxx.js)
515515
516516
ERROR in ./index.js (./loader!./index.js)
517-
× stack1stack2
517+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
518518
519519
Rspack x.x.x compiled with 2 errors in X.23
520520
@@ -523,10 +523,10 @@ assets by status xx bytes [cached] 1 asset
523523
524524
ERROR in ./index.js (./loader!./index.js)
525525
× stack1stack2
526-
stack3
526+
stack3loader error1 (from: Xdir/errors-xxx.js)
527527
528528
ERROR in ./index.js (./loader!./index.js)
529-
× stack1stack2
529+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
530530
531531
Rspack x.x.x compiled with 2 errors in X.23
532532
@@ -535,10 +535,10 @@ assets by status xx bytes [cached] 1 asset
535535
536536
ERROR in ./index.js (./loader!./index.js)
537537
× stack1stack2
538-
stack3
538+
stack3loader error1 (from: Xdir/errors-xxx.js)
539539
540540
ERROR in ./index.js (./loader!./index.js)
541-
× stack1stack2
541+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
542542
543543
Rspack x.x.x compiled with 2 errors in X.23
544544
@@ -547,10 +547,10 @@ assets by status xx bytes [cached] 1 asset
547547
548548
ERROR in ./index.js (./loader!./index.js)
549549
× stack1stack2
550-
stack3
550+
stack3loader error1 (from: Xdir/errors-xxx.js)
551551
552552
ERROR in ./index.js (./loader!./index.js)
553-
× stack1stack2
553+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
554554
555555
Rspack x.x.x compiled with 2 errors in X.23
556556
@@ -559,10 +559,10 @@ assets by status xx bytes [cached] 1 asset
559559
560560
ERROR in ./index.js (./loader!./index.js)
561561
× stack1stack2
562-
stack3
562+
stack3loader error1 (from: Xdir/errors-xxx.js)
563563
564564
ERROR in ./index.js (./loader!./index.js)
565-
× stack1stack2
565+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
566566
567567
Rspack x.x.x compiled with 2 errors in X.23
568568
@@ -571,10 +571,10 @@ assets by status xx bytes [cached] 1 asset
571571
572572
ERROR in ./index.js (./loader!./index.js)
573573
× stack1stack2
574-
stack3
574+
stack3loader error1 (from: Xdir/errors-xxx.js)
575575
576576
ERROR in ./index.js (./loader!./index.js)
577-
× stack1stack2
577+
× stack1stack2loader error2 (from: Xdir/errors-xxx.js)
578578
579579
Rspack x.x.x compiled with 2 errors in X.23"
580580
`;

0 commit comments

Comments
 (0)