Skip to main content

kmip_protocol/net/common/
error.rs

1use std::sync::PoisonError;
2
3/// There was a problem sending/receiving a KMIP request/response.
4#[non_exhaustive]
5#[derive(Debug)]
6pub enum NetError {
7    AuthenticationError,
8    ConfigurationError(String),
9    SerializeError(String),
10    IoError(std::io::Error),
11    DeserializeError {
12        err: String,
13
14        /// The KMIP TTLV wire bytes of the request related to the response
15        /// that could not be deserialized.
16        req: Box<[u8]>,
17
18        /// The KMIP TTLV wire bytes of the response that could not be
19        /// deserialized.
20        res: Box<[u8]>,
21    },
22    ServerError(String),
23    InternalError(String),
24    ItemNotFound(String),
25    UnexpectedData(String),
26    Unknown(String),
27}
28
29impl NetError {
30    pub fn deserialize_error(err: String) -> Self {
31        Self::DeserializeError {
32            err,
33            req: Default::default(),
34            res: Default::default(),
35        }
36    }
37
38    /// Is this a possibly transient problem with the connection to the server?
39    pub fn is_connection_error(&self) -> bool {
40        matches!(self, Self::IoError(_))
41    }
42}
43
44impl std::error::Error for NetError {}
45
46impl From<std::io::Error> for NetError {
47    fn from(err: std::io::Error) -> Self {
48        NetError::IoError(err)
49    }
50}
51
52/// Format the error for user-facing output.
53///
54/// Tip: examples/hex_to_txt.rs can be used to render KMIP TTLV protocol wire
55/// request/response bytes into a form that can be more easily understood.
56impl std::fmt::Display for NetError {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            NetError::AuthenticationError => f.write_str("Authentication error"),
60            NetError::ConfigurationError(err) => write!(f, "Configuration error: {err}"),
61            NetError::SerializeError(err) => write!(f, "Serialize error: {err}"),
62            NetError::IoError(err) => write!(f, "I/O error: {err}"),
63            NetError::DeserializeError { err, req, res } => write!(
64                f,
65                "Deserialize error: {err}\nRequest: {}\nResponse: {}",
66                hex::encode_upper(req),
67                hex::encode_upper(res)
68            ),
69            NetError::ServerError(err) => write!(f, "Server error: {err}"),
70            NetError::InternalError(err) => write!(f, "Internal error: {err}"),
71            NetError::ItemNotFound(err) => write!(f, "Item not found: {err}"),
72            NetError::Unknown(err) => write!(f, "Unknown error: {err}"),
73            NetError::UnexpectedData(data) => write!(f, "Unexpected data: {data}"),
74        }
75    }
76}
77
78/// The successful or failed outcome resulting from sending a request to a KMIP server.
79pub type NetResult<T> = std::result::Result<T, NetError>;
80
81impl<T> From<PoisonError<T>> for NetError {
82    fn from(err: PoisonError<T>) -> Self {
83        NetError::InternalError(err.to_string())
84    }
85}