Closed dherman closed 3 years ago
A user may choose not to operate on the Result
of a try_catch
. With the current design, a small amount of boilerplate to transform the Result<Handle<'a, T>, JsValue<'a>>
into a JsResult<'a, T>
for returning from a method:
let value = match result {
Ok(v) => Ok(v),
Err(err) => cx.throw(err)?,
};
I propose adding to this RFC an implementation of JsResultExt
to remove ease this boilerplate:
impl <'a, V, E> JsResultExt<'a, V> for Result<Handle<'a, V>, Handle<'a, E>>
where
V: Value,
E: Value,
{
fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, V> {
match self {
Ok(v) => Ok(v),
Err(err) => cx.throw(err),
}
}
}
This would simplify to:
let value = result.or_throw(value)?;
Notes from feature flagged implementation:
UnwindSafe
for legacy)'b: 'a
)panic
behavior on a manually constructed Err(Throw)
This RFC proposes a new
Context
method for catching any JavaScript exceptions that may be thrown during the execution of a computation. The result of the API uses Rust's standardResult
enum to distinguish between normal return values and exceptional values:Rendered