Replace panicking unwrap with descriptive errors in plugin-api#1134
Conversation
jeffcharles
left a comment
There was a problem hiding this comment.
You can run make ci to ensure your code will pass the linter and tests.
crates/plugin-api/src/lib.rs
Outdated
| // bytecode than if it were set to `false`. | ||
| let runtime = unsafe { RUNTIME.get().unwrap() }; | ||
| let runtime = unsafe { RUNTIME.get() } | ||
| .ok_or_else(|| anyhow!("Javy runtime not initialized. Ensure `initialize_runtime` is called before `compile_src`."))?; |
There was a problem hiding this comment.
We document requiring running javy init-plugin ... here so I'd recommend updating the error messages to include that javy init-plugin should be invoked. init-plugin initializes the runtime and saves the runtime into a series of data segments in the Wasm module that it outputs that RUNTIME references. Hypothetically someone could use this as a Rust library but we recommend using the lower-level javy crate for that use-case.
There was a problem hiding this comment.
Good call — updated both error messages to reference javy init-plugin instead. Thanks for the pointer to the extending docs.
e3dffcc to
be28bd7
Compare
…invoke compile_src() and invoke() panic with an opaque "called Option::unwrap() on a None value" when RUNTIME has not been initialized. This makes debugging difficult for plugin authors and host integrators, as the panic message gives no indication of what went wrong or how to fix it. Replace the bare .unwrap() calls with .ok_or_else() returning a descriptive anyhow error that names the uninitialized runtime and points to initialize_runtime as the required precondition. Both functions already return Result, so this is a non-breaking change.
be28bd7 to
8dd2e0c
Compare
|
Looks like one of the integration tests is failing due to no longer trapping when the plugin is uninitialized. The test should be updated to read from the linear memory at the offset given by the return value. The first 4 bytes is the result variant which should be a non-zero value to indicate an error state. |
|
Thanks @jeffcharles for the pointer! Updated the test to read the result discriminant from linear memory instead of expecting a trap. Should be green now. |
Now that compile_src returns a Result instead of panicking, the compile-src export no longer traps on an uninitialized runtime. Update the test to read the result discriminant from linear memory and assert a non-zero (error) value.
|
I'm seeing a formatting failure in CI. Again, would recommend running Again though, thank you for submitting this PR! The better error message should help steer other plugin developers toward running init-plugin. |
26c68c0 to
f51dba8
Compare
|
The failure was just cargo fmt wanting the .get_memory() chain split across lines. CI should be green now. No problem, just happy to contribute. Cheers! |
jeffcharles
left a comment
There was a problem hiding this comment.
Thank you again, this is a much nicer user experience and also helpful for providing direct feedback to LLMs
Summary
compile_src()andinvoke()injavy-plugin-apicall.unwrap()onRUNTIME.get(), which panics with an opaque message when the runtime has not been initialized:This makes debugging difficult for plugin authors and host integrators, as the panic message gives no indication of what went wrong or how to fix it.
Changes
Replace the bare
.unwrap()calls with.ok_or_else()returning a descriptiveanyhowerror that names the uninitialized runtime and points toinitialize_runtimeas the required precondition:Both functions already return
Result<T>, so this is a non-breaking change with no new dependencies.Context
This issue was encountered while building a Chainlink CRE workflow plugin. The javy host (v5.0.4) called
compile_srcbeforeinitialize_runtime, producing the opaque panic. The resulting error message (called Option::unwrap() on a None value) with a path pointing into the Cargo registry gave no actionable information about the root cause.