neon

Module result

Source
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)?;
    obj.prop(&mut cx, "message").get()
}

Structs§

  • A unit type indicating that the JavaScript thread is throwing an exception.

Traits§

Type Aliases§