kmip_protocol/net/common/
error.rs1use std::sync::PoisonError;
2
3#[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 req: Box<[u8]>,
17
18 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 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
52impl 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
78pub 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}