1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Transport related error
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[cfg(feature = "p2p-base")]
    #[error("libp2p transport error: {0:?}")]
    Libp2pTransport(#[source] std::sync::Arc<dyn std::error::Error + Send + Sync + 'static>),

    #[error("Error in capnp serialization: {0}")]
    Serialization(#[from] exocore_protos::capnp::Error),

    #[error("Field is not in capnp schema: code={0}")]
    SerializationNotInSchema(u16),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Could not upgrade a weak reference")]
    Upgrade,

    #[error("Try to lock a mutex that was poisoned")]
    Poisoned,

    #[error("An error occurred: {0}")]
    Other(String),
}

#[cfg(feature = "p2p-base")]
impl<Terr> From<libp2p::core::transport::TransportError<Terr>> for Error
where
    Terr: std::error::Error + Send + Sync + 'static,
{
    fn from(err: libp2p::core::transport::TransportError<Terr>) -> Self {
        Error::Libp2pTransport(std::sync::Arc::new(err))
    }
}

impl From<exocore_protos::capnp::NotInSchema> for Error {
    fn from(err: exocore_protos::capnp::NotInSchema) -> Self {
        Error::SerializationNotInSchema(err.0)
    }
}

impl<T> From<std::sync::PoisonError<T>> for Error {
    fn from(_err: std::sync::PoisonError<T>) -> Self {
        Error::Poisoned
    }
}