relay_ffi

Function with_last_error

source
pub fn with_last_error<R, F>(f: F) -> Option<R>
where F: FnOnce(&Error) -> R,
Expand description

Acquires a reference to the last error and passes it to the callback, if any.

Returns Some(R) if there was an error, otherwise None. The error resets when it is taken with take_last_error.

ยงExample

use relay_ffi::{catch_unwind, with_last_error};

#[catch_unwind]
unsafe fn run_ffi() -> i32 {
    "invalid".parse()?
}

let parsed = unsafe { run_ffi() };
match with_last_error(|e| e.to_string()) {
    Some(error) => println!("errored with: {error}"),
    None => println!("result: {parsed}"),
}