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
145pub unsafe fn is_error(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter) -> bool {
146    if obj_ref.is_object() {
147        #[cfg(feature = "bellard")]
148        {
149            let res = q::JS_IsError(context, *obj_ref.borrow_value());
150            res != 0
151        }
152        #[cfg(feature = "quickjs-ng")]
153        {
154            q::JS_IsError(context, *obj_ref.borrow_value())
155        }
156    } else {
157        false
158    }
159}
160
161pub fn get_stack(realm: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, JsError> {
162    let e = realm.invoke_function_by_name(&[], "Error", &[])?;
163    realm.get_object_property(&e, "stack")
164}
165
166/// Throw an error and get an Exception JSValue to return from native methods
167/// # Safety
168/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
169pub unsafe fn throw(context: *mut q::JSContext, error: QuickJsValueAdapter) -> q::JSValue {
170    assert!(is_error(context, &error));
171    q::JS_Throw(context, error.clone_value_incr_rc());
172    q::JSValue {
173        u: q::JSValueUnion { int32: 0 },
174        tag: TAG_EXCEPTION,
175    }
176}
177
178#[cfg(test)]
179pub mod tests {
180    use crate::facades::tests::init_test_rt;
181    use crate::jsutils::{JsError, Script};
182    use crate::quickjs_utils::functions;
183    use crate::values::{JsValueConvertable, JsValueFacade};
184    use std::thread;
185    use std::time::Duration;
186
187    #[test]
188    fn test_ex_nat() {
189        // check if stacktrace is preserved when invoking native methods
190
191        let rt = init_test_rt();
192        let res = rt.eval_sync(
193            None,
194            Script::new(
195                "ex.js",
196                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
197            ),
198        );
199        let ex = res.expect_err("script should have failed;");
200
201        #[cfg(feature = "bellard")]
202        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
203        #[cfg(feature = "quickjs-ng")]
204        assert_eq!(ex.get_message(), "__c_v__ is not defined");
205    }
206
207    #[test]
208    fn test_ex_cause() {
209        // check if stacktrace is preserved when invoking native methods
210
211        let rt = init_test_rt();
212        let res = rt.eval_sync(
213            None,
214            Script::new(
215                "ex.ts",
216                r#"
217                let a = 2;
218                let b = 3;
219
220                function f1(a, b) {
221                    throw new Error('Could not f1', { cause: 'Sabotage here' });
222                }
223                function f2() {
224                    try {
225                        let r = f1(a, b);
226                    } catch(ex) {
227                        throw new Error('could not f2', { cause: ex});
228                    }
229                }
230                f2()
231                "#,
232            ),
233        );
234        let ex = res.expect_err("script should have failed;");
235
236        assert_eq!(ex.get_message(), "could not f2");
237
238        let complete_err = format!("{ex}");
239        assert!(complete_err.contains("Caused by: Error: Could not f1"));
240        assert!(complete_err.contains("Caused by: Sabotage here"));
241    }
242
243    #[test]
244    fn test_ex0() {
245        // check if stacktrace is preserved when invoking native methods
246
247        let rt = init_test_rt();
248        let res = rt.eval_sync(
249            None,
250            Script::new(
251                "ex.js",
252                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
253            ),
254        );
255        let ex = res.expect_err("script should have failed;");
256
257        #[cfg(feature = "bellard")]
258        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
259        #[cfg(feature = "quickjs-ng")]
260        assert_eq!(ex.get_message(), "__c_v__ is not defined");
261    }
262
263    #[test]
264    fn test_ex1() {
265        // check if stacktrace is preserved when invoking native methods
266
267        let rt = init_test_rt();
268        rt.set_function(&[], "test_consume", move |_realm, args| {
269            // args[0] is a function i'll want to call
270            let func_jsvf = &args[0];
271            match func_jsvf {
272                JsValueFacade::JsFunction { cached_function } => {
273                    let _ = cached_function.invoke_function_sync(vec![12.to_js_value_facade()])?;
274                    Ok(0.to_js_value_facade())
275                }
276                _ => Err(JsError::new_str("poof")),
277            }
278        })
279        .expect("could not set function");
280        let s_res = rt.eval_sync(
281            None,
282            Script::new(
283                "test_ex34245.js",
284                "let consumer = function() {
285        console.log('consuming');
286        throw new Error('oh dear stuff failed at line 3 in consumer');
287        };
288        console.log('calling consume from line 6');
289        let a = test_consume(consumer);
290        console.log('should never reach line 7 %s', a)",
291            ),
292        );
293        match s_res {
294            Ok(o) => {
295                log::info!("o = {}", o.stringify());
296            }
297            Err(e) => {
298                log::error!("script failed: {}", e);
299                log::error!("{}", e);
300            }
301        }
302
303        std::thread::sleep(Duration::from_secs(1));
304    }
305
306    #[test]
307    fn test_ex3() {
308        let rt = init_test_rt();
309        rt.eval_sync(
310            None,
311            Script::new(
312                "test_ex3.js",
313                r#"
314async function a() {
315    await b();
316}
317
318async function b() {
319    //await 1;
320    throw Error("poof");
321}
322
323a().catch((ex) => {
324    console.error(ex);
325});
326        "#,
327            ),
328        )
329        .expect("script failed");
330        thread::sleep(Duration::from_secs(1));
331    }
332
333    #[test]
334    fn test_ex_stack() {
335        let rt = init_test_rt();
336        rt.exe_rt_task_in_event_loop(|rt| {
337            let realm = rt.get_main_realm();
338            realm
339                .install_closure(
340                    &[],
341                    "myFunc",
342                    |_rt, realm, _this, _args| crate::quickjs_utils::errors::get_stack(realm),
343                    0,
344                )
345                .expect("could not install func");
346
347            let res = realm
348                .eval(Script::new(
349                    "runMyFunc.js",
350                    r#"
351                function a(){
352                    return b();                
353                }
354                function b(){
355                    return myFunc();
356                }
357                a()
358            "#,
359                ))
360                .expect("script failed");
361
362            log::info!("test_ex_stack res = {}", res.to_string().unwrap());
363        });
364    }
365
366    #[test]
367    fn test_ex2() {
368        // check if stacktrace is preserved when invoking native methods
369
370        //simple_logging::log_to_stderr(LevelFilter::Info);
371
372        let rt = init_test_rt();
373        rt.exe_rt_task_in_event_loop(|q_js_rt| {
374            let q_ctx = q_js_rt.get_main_realm();
375
376            q_ctx
377                .eval(Script::new(
378                    "test_ex2_pre.es",
379                    "console.log('before ex test');",
380                ))
381                .expect("test_ex2_pre failed");
382            {
383                let func_ref1 = q_ctx
384                    .eval(Script::new(
385                        "test_ex2f1.es",
386                        "(function(){\nconsole.log('running f1');});",
387                    ))
388                    .expect("script failed");
389                assert!(functions::is_function_q(q_ctx, &func_ref1));
390                let res = functions::call_function_q(q_ctx, &func_ref1, &[], None);
391                match res {
392                    Ok(_) => {}
393                    Err(e) => {
394                        log::error!("func1 failed: {}", e);
395                    }
396                }
397            }
398            // why the f does this fail with a stack overflow if i remove the block above?
399            let func_ref2 = q_ctx
400                .eval(Script::new(
401                    "test_ex2.es",
402                    r#"
403                    const f = function(){
404                        throw Error('poof');
405                    };
406                    f
407                    "#,
408                ))
409                .expect("script failed");
410
411            assert!(functions::is_function_q(q_ctx, &func_ref2));
412            let res = functions::call_function_q(q_ctx, &func_ref2, &[], None);
413            match res {
414                Ok(_) => {}
415                Err(e) => {
416                    log::error!("func2 failed: {}", e);
417                }
418            }
419        });
420
421        #[cfg(feature = "bellard")]
422        {
423            let mjsvf = rt
424                .eval_module_sync(
425                    None,
426                    Script::new(
427                        "test_ex2.es",
428                        r#"
429                                throw Error('poof');
430                                "#,
431                    ),
432                )
433                .map_err(|e| {
434                    log::error!("script compilation failed: {e}");
435                    e
436                })
437                .expect("script compilation failed");
438            match mjsvf {
439                JsValueFacade::JsPromise { cached_promise } => {
440                    let pres = cached_promise
441                        .get_promise_result_sync()
442                        .expect("promise timed out");
443                    match pres {
444                        Ok(m) => {
445                            log::info!("prom resolved to {}", m.stringify())
446                        }
447                        Err(e) => {
448                            log::info!("prom rejected to {}", e.stringify())
449                        }
450                    }
451                }
452                _ => {
453                    panic!("not a prom")
454                }
455            }
456        }
457
458        std::thread::sleep(Duration::from_secs(1));
459    }
460}