ergo_lib_c_core/
error.rs

1#[cfg(feature = "rest")]
2use ergo_lib::ergo_rest::{NodeError, PeerDiscoveryError};
3
4use std::error;
5use thiserror::Error;
6
7// pub type Result<T> = result::Result<T, Box<dyn error::Error + 'static>>;
8
9#[derive(Error, Debug)]
10pub enum Error {
11    #[error("error: {0}")]
12    Misc(Box<dyn error::Error + 'static>),
13    #[error("invalid argument: {0}")]
14    InvalidArgument(&'static str),
15}
16
17pub type ErrorPtr = *mut Error;
18
19impl Error {
20    pub fn misc<E>(details: E) -> Self
21    where
22        E: error::Error + 'static,
23    {
24        Error::Misc(Box::new(details))
25    }
26
27    pub fn c_api_from(result: Result<(), Error>) -> ErrorPtr {
28        match result {
29            Ok(()) => std::ptr::null_mut(),
30            Err(err) => Box::into_raw(Box::new(err)),
31        }
32    }
33}
34
35#[cfg(feature = "rest")]
36impl From<NodeError> for Error {
37    fn from(error: NodeError) -> Self {
38        Error::Misc(format!("{:?}", error).into())
39    }
40}
41
42#[cfg(feature = "rest")]
43impl From<PeerDiscoveryError> for Error {
44    fn from(error: PeerDiscoveryError) -> Self {
45        Error::Misc(format!("{:?}", error).into())
46    }
47}