Skip to content

Commit 1e3d65c

Browse files
committed
fix: make sure we don't panic when an exception is thrown in an async context
1 parent 06bd074 commit 1e3d65c

File tree

8 files changed

+1149
-4
lines changed

8 files changed

+1149
-4
lines changed

crates/core/src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@ static CALL_ARGS: std::sync::Mutex<Vec<Vec<ArgType>>> = std::sync::Mutex::new(ve
1818
fn check_exception(this: &Ctx, err: rquickjs::Error) -> anyhow::Error {
1919
let s = match err {
2020
rquickjs::Error::Exception => {
21-
let err = this.catch().into_exception().unwrap();
22-
let msg = err.message().unwrap_or_default();
23-
format!("Exception: {}\n{}", msg, err.stack().unwrap_or_default())
21+
// Attempt to catch the exception
22+
let exception_result = this.catch();
23+
24+
// Check if we got a valid exception or if it's uninitialized (possibly due to async code)
25+
// - https://github.com/DelSkayn/rquickjs/issues/421
26+
// - https://github.com/DelSkayn/rquickjs/pull/422
27+
// - https://github.com/quickjs-ng/quickjs/issues/39
28+
// - https://github.com/quickjs-ng/quickjs/pull/1038
29+
30+
match exception_result.into_exception() {
31+
Some(err) => {
32+
let msg = err.message().unwrap_or_default();
33+
let stack = err.stack().unwrap_or_default();
34+
format!("Exception: {}\n{}", msg, stack)
35+
}
36+
None => {
37+
format!(
38+
"Uncaught exception in async context. \
39+
Async/Promise operations are not supported in this environment. \
40+
Please use only synchronous code."
41+
)
42+
}
43+
}
2444
}
2545
err => err.to_string(),
2646
};
@@ -102,7 +122,7 @@ fn invoke<'a, T, F: for<'b> Fn(Ctx<'b>, Value<'b>) -> T>(
102122
let function_invocation_result = function.call_arg(args);
103123

104124
while ctx.execute_pending_job() {
105-
continue
125+
continue;
106126
}
107127

108128
match function_invocation_result {

examples/issue_134/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

examples/issue_134/esbuild.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const esbuild = require('esbuild');
2+
3+
esbuild
4+
.build({
5+
entryPoints: ['src/index.ts'],
6+
outdir: 'dist',
7+
bundle: true,
8+
sourcemap: true,
9+
minify: false, // might want to use true for production build
10+
format: 'cjs', // needs to be CJS for now
11+
target: ['es2020'] // don't go over es2020 because quickjs doesn't support it
12+
})

0 commit comments

Comments
 (0)