Module neon::result [−][src]
Expand description
Represents JavaScript exceptions as a Rust Result
type.
Most interactions with the JavaScript engine can throw a JavaScript exception. Neon APIs
that can throw an exception are called throwing APIs and return the type
NeonResult
(or its shorthand JsResult
).
When a throwing API triggers a JavaScript exception, it returns an Err
result. This indicates that the thread associated with the Context
is now throwing, and allows Rust code to perform any cleanup. See the
neon::context
module documentation for more about
contexts and exceptions.
Typically, Neon code can manage JavaScript exceptions correctly and conveniently by
using Rust’s question mark (?
) operator. This ensures that Rust code
“short-circuits” when an exception is thrown and returns back to JavaScript without
calling any throwing APIs.
Example
Neon functions typically use JsResult
for their return type. This
example defines a function that extracts a property called "message"
from an object,
throwing an exception if the argument is not of the right type or extracting the property
fails:
fn get_message(mut cx: FunctionContext) -> JsResult<JsValue> {
let obj: Handle<JsObject> = cx.argument(0)?;
let prop: Handle<JsValue> = obj.get(&mut cx, "message")?;
Ok(prop)
}
Structs
Traits
Extension trait for converting Rust Result
values
into JsResult
values by throwing JavaScript exceptions.
Extension trait for converting Rust Result
values
into NeonResult
values by throwing JavaScript exceptions.
Type Definitions
Shorthand for a NeonResult
that produces JavaScript values.
The result type for throwing APIs.