prebindgen_jni_runtime/jni_binding_error.rs
1//! Framework error type for the JNI binding's error channel.
2//!
3//! A fallible `#[prebindgen] fn f(...) -> Result<T, E>` is delivered to the
4//! foreign side as `Result<T, JniBindingError<E>>`:
5//!
6//! * [`JniBindingError::JniError`] — a **binding** failure (UTF-8 decode of a
7//! `JString`, `instanceof`/null check, struct field read, handle wrap, closed
8//! handle, …). Framework-built converters compose their `?` failures here via
9//! `From<String>`; they are E-agnostic and use `JniBindingError<()>`.
10//! * [`JniBindingError::UserError`] — the function's **domain** error `E` (e.g.
11//! `zenoh::Error`). The `Result<T, E>` peel surfaces it on its own arm.
12//!
13//! The generated wrapper's error callback receives a fixed first `je: String?`
14//! (the binding message, set only on `JniError`) plus the domain error
15//! converted/deconstructed into one or more leaves (set only on `UserError`).
16//! `JniGenBuilder::new()` pre-registers this type so `__JniErr` (= `JniBindingError<()>`)
17//! is always available to framework converter bodies.
18
19/// Framework error type for the JNI binding's error channel. `T` is the
20/// function's domain error (`()` for the E-agnostic framework converters, whose
21/// failures are always [`Self::JniError`]).
22#[derive(Clone)]
23pub enum JniBindingError<T> {
24 /// A binding-layer failure, carrying a context message.
25 JniError(String),
26 /// The wrapped function's domain error.
27 UserError(T),
28}
29
30impl<T> From<String> for JniBindingError<T> {
31 fn from(s: String) -> Self {
32 JniBindingError::JniError(s)
33 }
34}
35
36// `Display`/`Debug` are unconditional in `T` (no `T: Display` bound) so the
37// framework's `__JniErr = JniBindingError<()>` (binding errors only) satisfies
38// them. The `UserError` arm is never displayed in practice — the domain error is
39// decomposed via its `convert_error`/`deconstruct_error` plan, not stringified.
40impl<T> core::fmt::Display for JniBindingError<T> {
41 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42 match self {
43 JniBindingError::JniError(s) => f.write_str(s),
44 JniBindingError::UserError(_) => f.write_str("<user error>"),
45 }
46 }
47}
48
49impl<T> core::fmt::Debug for JniBindingError<T> {
50 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51 match self {
52 JniBindingError::JniError(s) => write!(f, "JniError({s:?})"),
53 JniBindingError::UserError(_) => f.write_str("UserError(..)"),
54 }
55 }
56}