Errors
Rust represents fallible operations with Result<T, E>. Target languages have different conventions: Swift uses throws, Kotlin, Java, and C# use exceptions, and TypeScript uses try/catch. BoltFFI converts Result return types into the native error handling mechanism of each platform. When a function returns Err, it becomes a thrown error or exception in the target language.
Supported Error Types
The error type in Result<T, E> can be:
Stringor&'static str- becomes a generic error with a message- A struct marked with
#[error]- becomes a structured error type - An enum marked with
#[error]- becomes an error enum
The #[error] attribute marks types as error types. Generated bindings expose them through each target’s native error model:
- Swift: error types conform to
Error - Kotlin: error types extend
Exception - Java: error enums and classes include a nested
Exceptiontype that extendsRuntimeException - C#: generated methods throw
BoltExceptionfor string errors or typed*Exceptionwrappers that expose the original error value
String Errors
String errors use generic wrapper types:
- Swift:
FfiError - Kotlin:
FfiException - Java:
RuntimeException - C#:
BoltException - TypeScript:
FfiException
Custom error types are thrown directly or wrapped as the native target requires.
The simplest approach is returning Result<T, String> or Result<T, &'static str>. The error message is captured in a generic error type.
Struct Errors
For structured error information, define a struct with #[error]. The struct becomes a throwable error type in both languages.
Enum Errors
Error enums let you represent distinct failure cases. Simple enums (no associated data) become native enums. Enums with payloads become sealed types.
Simple Enums
Enums with Payloads
When enum variants carry associated data, the error becomes a sealed type hierarchy.
Async Errors
Async functions that return Result work the same way. The error is delivered through the target language’s native error handling when the async operation completes.