Skip to main content

onlyne_net/
error.rs

1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum NetError {
5    PinMismatch {
6        expected: String,
7        got: String,
8    },
9    ProtocolVersion {
10        peer: u16,
11        expected: u16,
12    },
13    Unauthorized(String),
14    Rejected {
15        code: String,
16        message: String,
17    },
18    /// The transport died. The supervisor dials again.
19    Disconnected(String),
20    /// The handle sits between connections and accepts no frame.
21    NotReady,
22    /// The caller's own deadline elapsed while the request was in flight.
23    RequestTimeout,
24    HandshakeTimeout,
25    FrameTooLarge,
26    BadFrame,
27    Io(String),
28    Crypto(String),
29    MalformedKey(String),
30}
31
32impl fmt::Display for NetError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::PinMismatch { expected, got } => write!(
36                f,
37                "certificate pin mismatch: expected {expected}, got {got}"
38            ),
39            Self::ProtocolVersion { peer, expected } => write!(
40                f,
41                "protocol version mismatch: peer {peer}, expected {expected}"
42            ),
43            Self::Unauthorized(detail) => write!(f, "unauthorized: {detail}"),
44            Self::Rejected { code, message } => write!(f, "rejected ({code}): {message}"),
45            Self::Disconnected(detail) => write!(f, "disconnected: {detail}"),
46            Self::NotReady => f.write_str("the connection is not ready"),
47            Self::RequestTimeout => f.write_str("request timed out"),
48            Self::HandshakeTimeout => f.write_str("handshake timed out"),
49            Self::FrameTooLarge => f.write_str("frame too large"),
50            Self::BadFrame => f.write_str("bad frame"),
51            Self::Io(detail) => write!(f, "I/O error: {detail}"),
52            Self::Crypto(detail) => write!(f, "cryptographic error: {detail}"),
53            Self::MalformedKey(detail) => write!(f, "malformed key: {detail}"),
54        }
55    }
56}
57
58impl std::error::Error for NetError {}
59
60impl From<std::io::Error> for NetError {
61    fn from(error: std::io::Error) -> Self {
62        Self::Io(error.to_string())
63    }
64}
65
66impl From<serde_json::Error> for NetError {
67    fn from(_error: serde_json::Error) -> Self {
68        Self::BadFrame
69    }
70}
71
72impl From<ed25519_dalek::SignatureError> for NetError {
73    fn from(error: ed25519_dalek::SignatureError) -> Self {
74        Self::Crypto(error.to_string())
75    }
76}