quickjs_runtime/quickjs_utils/
errors.rs

1//! utils for getting and reporting exceptions
2
3use crate::jsutils::JsError;
4use crate::quickjs_utils::{objects, primitives};
5use crate::quickjsrealmadapter::QuickJsRealmAdapter;
6use crate::quickjsvalueadapter::{QuickJsValueAdapter, TAG_EXCEPTION};
7use libquickjs_sys as q;
8
9/// Get the last exception from the runtime, and if present, convert it to an JsError.
10/// # Safety
11/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
12pub unsafe fn get_exception(context: *mut q::JSContext) -> Option<JsError> {
13    log::trace!("get_exception");
14    let exception_val = q::JS_GetException(context);
15    log::trace!("get_exception / 2");
16    let exception_ref =
17        QuickJsValueAdapter::new(context, exception_val, false, true, "errors::get_exception");
18
19    if exception_ref.is_null() {
20        None
21    } else {
22        let err = if exception_ref.is_exception() {
23            JsError::new_str("Could not get exception from runtime")
24        } else if exception_ref.is_object() {
25            error_to_js_error(context, &exception_ref)
26        } else {
27            match exception_ref.to_string() {
28                Ok(s) => JsError::new_string(s),
29                Err(ex) => {
30                    JsError::new_string(format!("Could not determine error due to error: {ex:?}"))
31                }
32            }
33        };
34        Some(err)
35    }
36}
37
38/// convert an instance of Error to JsError
39/// # Safety
40/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
41pub unsafe fn error_to_js_error(
42    context: *mut q::JSContext,
43    exception_ref: &QuickJsValueAdapter,
44) -> JsError {
45    log::trace!("error_to_js_error");
46    let name_ref = objects::get_property(context, exception_ref, "name")
47        .ok()
48        .unwrap();
49    let name_string = primitives::to_string(context, &name_ref).ok().unwrap();
50    let message_ref = objects::get_property(context, exception_ref, "message")
51        .ok()
52        .unwrap();
53    let message_string = primitives::to_string(context, &message_ref).ok().unwrap();
54    let stack_ref = objects::get_property(context, exception_ref, "stack")
55        .ok()
56        .unwrap();
57    let mut stack_string = "".to_string();
58
59    let stack2_ref = objects::get_property(context, exception_ref, "stack2")
60        .ok()
61        .unwrap();
62    if stack2_ref.is_string() {
63        stack_string.push_str(
64            primitives::to_string(context, &stack2_ref)
65                .ok()
66                .unwrap()
67                .as_str(),
68        );
69    }
70
71    if stack_ref.is_string() {
72        let stack_str = primitives::to_string(context, &stack_ref).ok().unwrap();
73        #[cfg(feature = "typescript")]
74        let stack_str = crate::typescript::unmap_stack_trace(stack_str.as_str());
75
76        stack_string.push_str(stack_str.as_str());
77    }
78
79    let cause_ref = objects::get_property(context, exception_ref, "cause")
80        .ok()
81        .unwrap();
82    // add cause as cause but also extend the stack trace with the cause stack trace
83
84    if cause_ref.is_null_or_undefined() {
85        JsError::new(name_string, message_string, stack_string)
86    } else {
87        QuickJsRealmAdapter::with_context(context, |realm| {
88            let cause_str = cause_ref.to_string().ok().unwrap();
89            let cause_jsvf = realm.to_js_value_facade(&cause_ref).ok().unwrap();
90
91            stack_string.push_str("Caused by: ");
92            stack_string.push_str(cause_str.as_str());
93
94            JsError::new2(name_string, message_string, stack_string, cause_jsvf)
95        })
96    }
97}
98
99/// Create a new Error object
100/// # Safety
101/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
102pub unsafe fn new_error(
103    context: *mut q::JSContext,
104    name: &str,
105    message: &str,
106    stack: &str,
107) -> Result<QuickJsValueAdapter, JsError> {
108    let obj = q::JS_NewError(context);
109    let obj_ref = QuickJsValueAdapter::new(
110        context,
111        obj,
112        false,
113        true,
114        format!("new_error {name}").as_str(),
115    );
116    objects::set_property(
117        context,
118        &obj_ref,
119        "message",
120        &primitives::from_string(context, message)?,
121    )?;
122    objects::set_property(
123        context,
124        &obj_ref,
125        "name",
126        &primitives::from_string(context, name)?,
127    )?;
128    objects::set_property(
129        context,
130        &obj_ref,
131        "stack2",
132        &primitives::from_string(context, stack)?,
133    )?;
134    Ok(obj_ref)
135}
136
137/// See if a JSValueRef is an Error object
138pub fn is_error_q(q_ctx: &QuickJsRealmAdapter, obj_ref: &QuickJsValueAdapter) -> bool {
139    unsafe { is_error(q_ctx.context, obj_ref) }
140}
141
142/// See if a JSValueRef is an Error object
143/// # Safety
144/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
145#[allow(unused_variables)]
146pub unsafe fn is_error(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter) -> bool {
147    if obj_ref.is_object() {
148        #[cfg(feature = "bellard")]
149        {
150            let res = q::JS_IsError(context, *obj_ref.borrow_value());
151            res != 0
152        }
153        #[cfg(feature = "quickjs-ng")]
154        {
155            q::JS_IsError(*obj_ref.borrow_value())
156        }
157    } else {
158        false
159    }
160}
161
162pub fn get_stack(realm: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, JsError> {
163    let e = realm.invoke_function_by_name(&[], "Error", &[])?;
164    realm.get_object_property(&e, "stack")
165}
166
167/// Throw an error and get an Exception JSValue to return from native methods
168/// # Safety
169/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
170pub unsafe fn throw(context: *mut q::JSContext, error: QuickJsValueAdapter) -> q::JSValue {
171    assert!(is_error(context, &error));
172    q::JS_Throw(context, error.clone_value_incr_rc());
173    q::JSValue {
174        u: q::JSValueUnion { int32: 0 },
175        tag: TAG_EXCEPTION,
176    }
177}
178
179#[cfg(test)]
180pub mod tests {
181    use crate::facades::tests::init_test_rt;
182    use crate::jsutils::{JsError, Script};
183    use crate::quickjs_utils::functions;
184    use crate::values::{JsValueConvertable, JsValueFacade};
185    use std::thread;
186    use std::time::Duration;
187
188    #[test]
189    fn test_ex_nat() {
190        // check if stacktrace is preserved when invoking native methods
191
192        let rt = init_test_rt();
193        let res = rt.eval_sync(
194            None,
195            Script::new(
196                "ex.js",
197                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
198            ),
199        );
200        let ex = res.expect_err("script should have failed;");
201
202        #[cfg(feature = "bellard")]
203        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
204        #[cfg(feature = "quickjs-ng")]
205        assert_eq!(ex.get_message(), "__c_v__ is not defined");
206    }
207
208    #[test]
209    fn test_ex_cause() {
210        // check if stacktrace is preserved when invoking native methods
211
212        let rt = init_test_rt();
213        let res = rt.eval_sync(
214            None,
215            Script::new(
216                "ex.ts",
217                r#"
218                let a = 2;
219                let b = 3;
220
221                function f1(a, b) {
222                    throw new Error('Could not f1', { cause: 'Sabotage here' });
223                }
224                function f2() {
225                    try {
226                        let r = f1(a, b);
227                    } catch(ex) {
228                        throw new Error('could not f2', { cause: ex});
229                    }
230                }
231                f2()
232                "#,
233            ),
234        );
235        let ex = res.expect_err("script should have failed;");
236
237        assert_eq!(ex.get_message(), "could not f2");
238
239        let complete_err = format!("{ex}");
240        assert!(complete_err.contains("Caused by: Error: Could not f1"));
241        assert!(complete_err.contains("Caused by: Sabotage here"));
242    }
243
244    #[test]
245    fn test_ex0() {
246        // check if stacktrace is preserved when invoking native methods
247
248        let rt = init_test_rt();
249        let res = rt.eval_sync(
250            None,
251            Script::new(
252                "ex.js",
253                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
254            ),
255        );
256        let ex = res.expect_err("script should have failed;");
257
258        #[cfg(feature = "bellard")]
259        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
260        #[cfg(feature = "quickjs-ng")]
261        assert_eq!(ex.get_message(), "__c_v__ is not defined");
262    }
263
264    #[test]
265    fn test_ex1() {
266        // check if stacktrace is preserved when invoking native methods
267
268        let rt = init_test_rt();
269        rt.set_function(&[], "test_consume", move |_realm, args| {
270            // args[0] is a function i'll want to call
271            let func_jsvf = &args[0];
272            match func_jsvf {
273                JsValueFacade::JsFunction { cached_function } => {
274                    let _ = cached_function.invoke_function_sync(vec![12.to_js_value_facade()])?;
275                    Ok(0.to_js_value_facade())
276                }
277                _ => Err(JsError::new_str("poof")),
278            }
279        })
280        .expect("could not set function");
281        let s_res = rt.eval_sync(
282            None,
283            Script::new(
284                "test_ex34245.js",
285                "let consumer = function() {
286        console.log('consuming');
287        throw new Error('oh dear stuff failed at line 3 in consumer');
288        };
289        console.log('calling consume from line 6');
290        let a = test_consume(consumer);
291        console.log('should never reach line 7 %s', a)",
292            ),
293        );
294        match s_res {
295            Ok(o) => {
296                log::info!("o = {}", o.stringify());
297            }
298            Err(e) => {
299                log::error!("script failed: {}", e);
300                log::error!("{}", e);
301            }
302        }
303
304        std::thread::sleep(Duration::from_secs(1));
305    }
306
307    #[test]
308    fn test_ex3() {
309        let rt = init_test_rt();
310        rt.eval_sync(
311            None,
312            Script::new(
313                "test_ex3.js",
314                r#"
315async function a() {
316    await b();
317}
318
319async function b() {
320    //await 1;
321    throw Error("poof");
322}
323
324a().catch((ex) => {
325    console.error(ex);
326});
327        "#,
328            ),
329        )
330        .expect("script failed");
331        thread::sleep(Duration::from_secs(1));
332    }
333
334    #[test]
335    fn test_ex_stack() {
336        let rt = init_test_rt();
337        rt.exe_rt_task_in_event_loop(|rt| {
338            let realm = rt.get_main_realm();
339            realm
340                .install_closure(
341                    &[],
342                    "myFunc",
343                    |_rt, realm, _this, _args| crate::quickjs_utils::errors::get_stack(realm),
344                    0,
345                )
346                .expect("could not install func");
347
348            let res = realm
349                .eval(Script::new(
350                    "runMyFunc.js",
351                    r#"
352                function a(){
353                    return b();                
354                }
355                function b(){
356                    return myFunc();
357                }
358                a()
359            "#,
360                ))
361                .expect("script failed");
362
363            log::info!("test_ex_stack res = {}", res.to_string().unwrap());
364        });
365    }
366
367    #[test]
368    fn test_ex2() {
369        // check if stacktrace is preserved when invoking native methods
370
371        //simple_logging::log_to_stderr(LevelFilter::Info);
372
373        let rt = init_test_rt();
374        rt.exe_rt_task_in_event_loop(|q_js_rt| {
375            let q_ctx = q_js_rt.get_main_realm();
376
377            q_ctx
378                .eval(Script::new(
379                    "test_ex2_pre.es",
380                    "console.log('before ex test');",
381                ))
382                .expect("test_ex2_pre failed");
383            {
384                let func_ref1 = q_ctx
385                    .eval(Script::new(
386                        "test_ex2f1.es",
387                        "(function(){\nconsole.log('running f1');});",
388                    ))
389                    .expect("script failed");
390                assert!(functions::is_function_q(q_ctx, &func_ref1));
391                let res = functions::call_function_q(q_ctx, &func_ref1, &[], None);
392                match res {
393                    Ok(_) => {}
394                    Err(e) => {
395                        log::error!("func1 failed: {}", e);
396                    }
397                }
398            }
399            // why the f does this fail with a stack overflow if i remove the block above?
400            let func_ref2 = q_ctx
401                .eval(Script::new(
402                    "test_ex2.es",
403                    r#"
404                    const f = function(){
405                        throw Error('poof');
406                    };
407                    f
408                    "#,
409                ))
410                .expect("script failed");
411
412            assert!(functions::is_function_q(q_ctx, &func_ref2));
413            let res = functions::call_function_q(q_ctx, &func_ref2, &[], None);
414            match res {
415                Ok(_) => {}
416                Err(e) => {
417                    log::error!("func2 failed: {}", e);
418                }
419            }
420        });
421
422        #[cfg(feature = "bellard")]
423        {
424            let mjsvf = rt
425                .eval_module_sync(
426                    None,
427                    Script::new(
428                        "test_ex2.es",
429                        r#"
430                                throw Error('poof');
431                                "#,
432                    ),
433                )
434                .map_err(|e| {
435                    log::error!("script compilation failed: {e}");
436                    e
437                })
438                .expect("script compilation failed");
439            match mjsvf {
440                JsValueFacade::JsPromise { cached_promise } => {
441                    let pres = cached_promise
442                        .get_promise_result_sync()
443                        .expect("promise timed out");
444                    match pres {
445                        Ok(m) => {
446                            log::info!("prom resolved to {}", m.stringify())
447                        }
448                        Err(e) => {
449                            log::info!("prom rejected to {}", e.stringify())
450                        }
451                    }
452                }
453                _ => {
454                    panic!("not a prom")
455                }
456            }
457        }
458
459        std::thread::sleep(Duration::from_secs(1));
460    }
461}