bitwarden_uniffi_error/
lib.rs1#![doc = include_str!("../README.md")]
2
3use std::sync::OnceLock;
4
5#[allow(clippy::type_complexity)]
6static ERROR_TO_UNIFFI_ERROR: OnceLock<
7 Box<dyn Fn(Box<dyn std::error::Error + Send + Sync>) -> anyhow::Error + Send + Sync + 'static>,
8> = OnceLock::new();
9
10pub use anyhow::Error;
11
12pub fn set_error_to_uniffi_error<F>(f: F)
17where
18 F: Fn(Box<dyn std::error::Error + Send + Sync>) -> anyhow::Error + Send + Sync + 'static,
19{
20 let _ = ERROR_TO_UNIFFI_ERROR.set(Box::new(f));
21}
22
23fn convert_error<E: std::error::Error + Send + Sync + 'static>(error: E) -> anyhow::Error {
24 if let Some(f) = ERROR_TO_UNIFFI_ERROR.get() {
25 f(Box::new(error))
26 } else {
27 anyhow::Error::new(error)
28 }
29}
30
31pub fn convert_result<T, E: std::error::Error + Send + Sync + 'static>(
35 result: Result<T, E>,
36) -> Result<T, anyhow::Error> {
37 result.map_err(|e| convert_error(e))
38}