bromine/
error.rs

1use crate::error_event::ErrorEventData;
2use thiserror::Error;
3use tokio::sync::oneshot;
4#[cfg(feature = "encryption_layer")]
5use x25519_dalek::PublicKey;
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Error, Debug)]
10pub enum Error {
11    #[error(transparent)]
12    IoError(#[from] tokio::io::Error),
13
14    #[cfg(feature = "serialize")]
15    #[error("failed to serialize event: {0}")]
16    Serialization(#[from] crate::payload::SerializationError),
17
18    #[error("build Error: {0}")]
19    BuildError(String),
20
21    #[error("{0}")]
22    Message(String),
23
24    #[error("channel Error: {0}")]
25    ReceiveError(#[from] oneshot::error::RecvError),
26
27    #[error("the received event was corrupted")]
28    CorruptedEvent,
29
30    #[error("send Error")]
31    SendError,
32
33    #[error("received error response: {0}")]
34    ErrorEvent(#[from] ErrorEventData),
35
36    #[error("timed out")]
37    Timeout,
38
39    #[error("Unsupported API Version {0}")]
40    UnsupportedVersion(String),
41
42    #[error("Invalid state")]
43    InvalidState,
44
45    #[cfg(feature = "encryption_layer")]
46    #[error("Connection of unknown peer with key {0:?} refused")]
47    UnknownPeer(PublicKey),
48}
49
50impl Error {
51    pub fn unsupported_version_vec(version: Vec<u8>) -> Self {
52        let mut version_string = version
53            .into_iter()
54            .fold(String::new(), |acc, val| format!("{}{}.", acc, val));
55        version_string.pop();
56
57        Self::UnsupportedVersion(version_string)
58    }
59}
60
61impl From<String> for Error {
62    fn from(s: String) -> Self {
63        Error::Message(s)
64    }
65}
66
67impl From<&str> for Error {
68    fn from(s: &str) -> Self {
69        Error::Message(s.to_string())
70    }
71}