Skip to main content

ErrorReport

Trait ErrorReport 

Source
pub trait ErrorReport {
    // Required method
    fn message(&self) -> String;

    // Provided methods
    fn code(&self) -> i32 { ... }
    fn payload(&self) -> Vec<u8> ⓘ { ... }
}
Expand description

Maps a producer error onto the ABI’s (code, message, payload) triple.

A fallible #[weaveffi::export] function returning Result<T, E> reports Err(e) through its trailing out_err slot using this trait. String and &str errors are covered out of the box with the generic code -1, so Result<T, String> needs no extra code. The #[weaveffi::error] expansion implements it for an error domain’s enum: the code is the variant’s discriminant, the message is the enum’s Display output, and the payload is the variant’s fields.

To implement the trait by hand:

use weaveffi::abi::ErrorReport;

enum KvError {
    KeyNotFound,
    Io(String),
}

impl ErrorReport for KvError {
    fn code(&self) -> i32 {
        match self {
            KvError::KeyNotFound => 1001,
            KvError::Io(_) => 1004,
        }
    }
    fn message(&self) -> String {
        match self {
            KvError::KeyNotFound => "key not found".to_string(),
            KvError::Io(detail) => format!("I/O error: {detail}"),
        }
    }
}

Required Methods§

Source

fn message(&self) -> String

The human-readable message written to FfiError::message.

Provided Methods§

Source

fn code(&self) -> i32

The non-zero status code written to FfiError::code. Defaults to GENERIC_ERROR_CODE.

Source

fn payload(&self) -> Vec<u8> ⓘ

The serialized payload fields written to FfiError::payload_ptr, or an empty vector for a code with no fields.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ErrorReport for &str

Source§

fn message(&self) -> String

Source§

impl ErrorReport for Box<dyn Error + Send + Sync>

Source§

fn message(&self) -> String

Source§

impl ErrorReport for Box<dyn Error>

Source§

fn message(&self) -> String

Source§

impl ErrorReport for String

Source§

fn message(&self) -> String

Implementors§

Source§

impl ErrorReport for ForeignError

A ForeignError is reported with its own code, message, and payload, so a producer function whose error type is ForeignError can propagate a callback failure with ?.