Use this page with an LLM
Errors
C examples use the package name demo and include demo.h. See C memory management for borrowed inputs and returned owners.
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 those targets. C returns a struct with an ok flag and a union holding the value or error; callers check the flag and free the result.
Supported Error Types
In C, read data.value when ok is true and data.error when it is false. Result<(), E> has no success value to read. The result owns whichever union member is active, including nested allocations or a returned class handle. Call the result’s generated cleanup function once; do not free the active member separately and then free the result.
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 - C: the typed result contains the error record, enum, or string in
data.error
String Errors
String errors use generic wrapper types:
- Swift:
FfiError - Kotlin:
FfiException - Java:
RuntimeException - C#:
BoltException - TypeScript:
FfiException - C:
DemoStringin the active result union for a package nameddemo
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 managed targets expose it through their error or exception types. C puts the error record in the result’s data.error field.
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 managed targets expose a sealed type hierarchy or tagged union. C uses the enum’s tag and data union inside the result’s data.error field.
Async Errors
C does not generate async functions yet. This section applies to the other targets.
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.
