Skip to content

Commit b64acf1

Browse files
authored
fix: make Error compatible with 0.1.0 (#9)
* fix: make Error compatible with 0.1.0 * fix: minimal import
1 parent 87d67f5 commit b64acf1

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

src/error.rs

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,25 @@ map_error!(deno_core::futures::channel::oneshot::Canceled, |e| {
222222
});
223223

224224
map_error!(deno_core::error::CoreError, |e| {
225-
Error::Runtime(e.to_string())
225+
// COMPATIBILITY FIX: Extract JsError from CoreError::Js variant to maintain compatibility with rustyscript 0.1.0
226+
//
227+
// In rustyscript 0.1.0, JavaScript errors (syntax errors, reference errors, etc.) returned Error::JsError.
228+
// In 0.2.0, the upgrade to deno_core 0.352.1 changed how CoreError is handled, causing these errors to
229+
// be returned as Error::Runtime instead of Error::JsError, breaking compatibility.
230+
//
231+
// This fix ensures that JavaScript errors wrapped in CoreError::Js are properly extracted and returned
232+
// as Error::JsError, maintaining the same error type behavior as version 0.1.0.
233+
//
234+
// Error types affected:
235+
// - SyntaxError (e.g., "let x = {")
236+
// - ReferenceError (e.g., "nonexistent_variable")
237+
// - TypeError (e.g., "null.method()")
238+
// - Other JavaScript runtime errors
239+
use deno_core::error::CoreError;
240+
match e {
241+
CoreError::Js(js_error) => Error::JsError(js_error),
242+
_ => Error::Runtime(e.to_string()),
243+
}
226244
});
227245

228246
#[cfg(feature = "broadcast_channel")]
@@ -275,14 +293,76 @@ mod test {
275293
let mut runtime = Runtime::new(RuntimeOptions::default()).unwrap();
276294

277295
let e = runtime.eval::<Undefined>("1+1;\n1 + x").unwrap_err().as_highlighted(ErrorFormattingOptions::default());
278-
assert_eq!(e, "ReferenceError: x is not defined\n at <anonymous>:2:5");
296+
// After fixing CoreError mapping, reference errors now correctly return as JsError with proper highlighting
297+
assert!(e.contains("ReferenceError: x is not defined"));
298+
assert!(e.contains("2:"));
279299

280300
let module = Module::new("test.js", "1+1;\n1 + x");
281301
let e = runtime.load_module(&module).unwrap_err().as_highlighted(ErrorFormattingOptions {
282302
include_filename: false,
283303
..Default::default()
284304
});
305+
// After fixing CoreError mapping, module errors now correctly return as JsError with proper highlighting
285306
assert!(e.contains("ReferenceError: x is not defined"));
286-
assert!(e.contains("test.js:2:5"));
307+
assert!(e.contains("At 2:"));
308+
}
309+
310+
#[test]
311+
fn test_error_type_compatibility() {
312+
use crate::Error;
313+
let mut runtime = Runtime::new(RuntimeOptions::default()).unwrap();
314+
315+
// Test syntax error - should return JsError for compatibility with 0.1.0
316+
match runtime.eval::<()>("let x = {") {
317+
Ok(_) => panic!("Expected syntax error"),
318+
Err(e) => {
319+
assert!(
320+
matches!(e, Error::JsError(_)),
321+
"Syntax errors should return Error::JsError for compatibility, got: {:?}",
322+
e
323+
);
324+
assert!(
325+
e.to_string().contains("SyntaxError")
326+
|| e.to_string().contains("Unexpected end")
327+
);
328+
}
329+
}
330+
331+
// Test reference error - should return JsError
332+
match runtime.eval::<()>("nonexistent_variable") {
333+
Ok(_) => panic!("Expected reference error"),
334+
Err(e) => {
335+
assert!(
336+
matches!(e, Error::JsError(_)),
337+
"Reference errors should return Error::JsError, got: {:?}",
338+
e
339+
);
340+
assert!(e.to_string().contains("not defined"));
341+
}
342+
}
343+
344+
// Test type error - should return JsError
345+
match runtime.eval::<()>("null.nonexistent_method()") {
346+
Ok(_) => panic!("Expected type error"),
347+
Err(e) => {
348+
assert!(
349+
matches!(e, Error::JsError(_)),
350+
"Type errors should return Error::JsError, got: {:?}",
351+
e
352+
);
353+
}
354+
}
355+
356+
// Test another syntax error pattern
357+
match runtime.eval::<()>("if (true") {
358+
Ok(_) => panic!("Expected syntax error"),
359+
Err(e) => {
360+
assert!(
361+
matches!(e, Error::JsError(_)),
362+
"Syntax errors should return Error::JsError, got: {:?}",
363+
e
364+
);
365+
}
366+
}
287367
}
288368
}

0 commit comments

Comments
 (0)