Skip to main content

weirdnet_net/
error.rs

1/// What went wrong. Applications map these onto their own errors; the messages say what
2/// happened without naming the application.
3#[derive(Debug, thiserror::Error)]
4pub enum NetError {
5    /// Bad input: a token, an offer or answer, a scope, a card, a request.
6    #[error("invalid {what}: {msg}")]
7    Invalid { what: &'static str, msg: String },
8    /// A signature, key or sealed message that does not check out.
9    #[error("crypto: {0}")]
10    Crypto(String),
11    /// The node, a transport, a peer that can't be reached, a timeout.
12    #[error("network: {0}")]
13    Network(String),
14    /// From the keys crate, unchanged.
15    #[error(transparent)]
16    Keys(#[from] weirdnet_keys::KeysError),
17    #[error(transparent)]
18    Json(#[from] serde_json::Error),
19    /// Something in the host's records that should not happen (a damaged or missing entry).
20    #[error("{0}")]
21    Other(String),
22}
23
24impl NetError {
25    pub fn invalid(what: &'static str, msg: impl Into<String>) -> Self {
26        NetError::Invalid { what, msg: msg.into() }
27    }
28    pub(crate) fn net(e: impl std::fmt::Display) -> Self {
29        NetError::Network(e.to_string())
30    }
31}
32
33pub type Result<T> = std::result::Result<T, NetError>;