When executing JavaScript code via rquickjs, errors can be either native Rust errors or exceptions thrown from within the QuickJS engine. To handle both, use CaughtResult<'js, T> (an alias for StdResult<T, CaughtError<'js>>).
CaughtError can be one of three variants:
Error(Error): A native Rust error.Exception(Exception<'js>): A JavaScript exception that is an instance of the Error object.Value(Value<'js>): A JavaScript exception that is a primitive value (e.g., throw 3).
You can use the catch extension trait to convert a standard Result<T> into a CaughtResult<'js, T>, which automatically retrieves the underlying JavaScript exception value from the context if an Error::Exception is encountered.
# use rquickjs::{Error, Context, Runtime, CaughtError};
# let rt = Runtime::new().unwrap();
# let ctx = Context::full(&rt).unwrap();
# ctx.with(|ctx|{
# use rquickjs::CatchResultExt;
if let Err(CaughtError::Value(err)) = ctx.eval::<(),_>("throw 3").catch(&ctx){
assert_eq!(err.as_int(), Some(3));
} else {
panic!("Expected a Value exception")
}