Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracing Errors in JS #386

Open
apexys opened this issue Nov 13, 2024 · 2 comments
Open

Tracing Errors in JS #386

apexys opened this issue Nov 13, 2024 · 2 comments

Comments

@apexys
Copy link

apexys commented Nov 13, 2024

I'm currently trying to use rquickjs to run the JS parts of https://github.com/pyodide/pyodide to be able to statically embed a python interpreter into a rust executable.

This is of course a very large codebase, but I'm trying an iterative approach, where I just patch in enough support so that I get the correct calls to the WebAssembly objects.

However, I often get stuck when some JS Exception occurs.

Is there a way to, when ctx.eval returns an error, to get a stack trace or the line of code that caused the problem?

This is especially a problem with Promises, since a failed Promise often only returns "WouldBlock", although the underlying problem is an exception that happened somewhere along the way (usually a missing import or a function not being defined yet).

My attempt at logging an error is as follows:

fn run_js(ctx: &Context, script: &str) {
    ctx.with(|ctx| {
        let res: Result<Value, rquickjs::Error> = ctx.eval(script);
        match res {
            Err(e) => {
                if e.is_exception() {
                    let exc = ctx.catch();
                    eprintln!("Exception during script evaluation: {exc:?}");
                } else {
                    eprintln!("Error evaluating script: {:?}", e)
                }
            }
            Ok(value) => {
                if value.is_promise() {
                    let p = value.into_promise().unwrap();
                    let v: Result<Value, rquickjs::Error> = p.finish();
                    if let Err(e) = v{
                        if e.is_exception() {
                            let exc = ctx.catch();
                            eprintln!("Exception during promise execution: {exc:?}");
                        } else {
                            eprintln!("Error during promise execution: {e:?} {p:?}");
                            let p = p.into_inner();
                            eprintln!("Promise inner: {:?}", p);
                            let p = p.into_inner();
                            eprintln!("Promise object inner: {:?}", p);
                            let prom = p.as_promise().unwrap();
                            let res: Result<Value, rquickjs::Error> = prom.finish();
                            eprintln!("Inner promise error: {res:?}");
                        }
                    } else {
                        eprintln!("Script evaluated with promise resulting in {:?}", v);
                    }
                } else {
                    eprintln!("Script evaluated with value {:?}", value);
                }
            }
        }
    });
}

This then prints something like

Error during promise execution: WouldBlock Promise(Object(Promise(0x62b51d48ce10)))
Promise inner: Object(Promise(0x62b51d48ce10))
Promise object inner: Promise(0x62b51d48ce10)
Inner promise error: Err(WouldBlock)

Thank you in advance!

@richarddd
Copy link
Contributor

I would recommend manually looping pending jobs instead.
I also suggested this PR that drives the schedular forward when promises are being polled:
#312

@apexys
Copy link
Author

apexys commented Nov 13, 2024

Thank you for your answer!

This is how I implemented the looping:

let p = value.into_promise().unwrap();
while p.state() == PromiseState::Pending{
   ctx.execute_pending_job();
   let exc = ctx.catch();
   if !exc.is_null(){
      eprintln!("Exception during promise execution: {exc:?}");
   }
}

I still get the same output.

I will also try the async runtime with your patch, but currently I'm trying this on the sync version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants