Skip to content

Commit

Permalink
update unhandled prom tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
andrieshiemstra committed Mar 15, 2024
1 parent a552cb9 commit 6119666
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.13.2 (WiP)
# 0.13.2

* add realm id to log on unhandled promises
* add stack to unhandled prom tracker

# 0.13.1

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quickjs_runtime"
version = "0.13.1"
version = "0.13.2"
authors = ["Andries Hiemstra <[email protected]>"]
edition = "2021"
description = "Wrapper API and utils for the QuickJS JavaScript engine with support for Promise, Modules, Async/await"
Expand Down
39 changes: 38 additions & 1 deletion src/quickjs_utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ pub unsafe fn is_error(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter
}
}

pub fn get_stack(realm: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, JsError> {
let e = realm.invoke_function_by_name(&[], "Error", &[])?;
realm.get_object_property(&e, "stack")
}

/// Throw an error and get an Exception JSValue to return from native methods
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
Expand All @@ -148,7 +153,6 @@ pub mod tests {
use crate::quickjs_utils::functions;
use crate::values::{JsValueConvertable, JsValueFacade};
use std::thread;
//use log::LevelFilter;
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -293,6 +297,39 @@ async function a(){
thread::sleep(Duration::from_secs(1));
}

#[test]
fn test_ex_stack() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|rt| {
let realm = rt.get_main_realm();
realm
.install_closure(
&[],
"myFunc",
|_rt, realm, _this, _args| crate::quickjs_utils::errors::get_stack(realm),
0,
)
.expect("could not install func");

let res = realm
.eval(Script::new(
"runMyFunc.js",
r#"
function a(){
return b();
}
function b(){
return myFunc();
}
a()
"#,
))
.expect("script failed");

log::info!("test_ex_stack res = {}", res.to_string().unwrap());
});
}

#[test]
fn test_ex2() {
// check if stacktrace is preserved when invoking native methods
Expand Down
19 changes: 14 additions & 5 deletions src/quickjs_utils/promises.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::jsutils::JsError;
use crate::quickjs_utils;
use crate::quickjs_utils::errors::get_stack;
use crate::quickjs_utils::functions;
use crate::quickjs_utils::objects::is_instance_of_by_name;
use crate::quickjsrealmadapter::QuickJsRealmAdapter;
Expand Down Expand Up @@ -243,20 +244,28 @@ unsafe extern "C" fn promise_rejection_tracker(
QuickJsRuntimeAdapter::do_with(|rt| {
let realm = rt.get_quickjs_context(ctx);
let realm_id = realm.get_realm_id();

let stack = match get_stack(realm) {
Ok(s) => match s.to_string() {
Ok(s) => s,
Err(_) => "".to_string(),
},
Err(_) => "".to_string(),
};
match reason_str_res {
Ok(reason_str) => {
log::error!(
"[{}] unhandled promise rejection, reason: {}",
"[{}] unhandled promise rejection, reason: {}{}",
realm_id,
reason_str
reason_str,
stack
);
}
Err(e) => {
log::error!(
"[{}] unhandled promise rejection, could not get reason: {}",
"[{}] unhandled promise rejection, could not get reason: {}{}",
realm_id,
e
e,
stack
);
}
}
Expand Down

0 comments on commit 6119666

Please sign in to comment.