epp_client/
error.rs

1//! Error types to wrap internal errors and make EPP errors easier to read
2
3use std::array::TryFromSliceError;
4use std::error::Error as StdError;
5use std::fmt::{self, Display};
6use std::io;
7use std::num::TryFromIntError;
8use std::str::Utf8Error;
9use std::string::FromUtf8Error;
10
11use crate::response::ResponseStatus;
12
13/// Error enum holding the possible error types
14#[derive(Debug)]
15pub enum Error {
16    Command(Box<ResponseStatus>),
17    Io(std::io::Error),
18    Timeout,
19    Xml(Box<dyn StdError + Send + Sync>),
20    Other(Box<dyn StdError + Send + Sync>),
21}
22
23impl StdError for Error {}
24
25impl Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::Command(e) => {
29                write!(f, "command error: {}", e.result.message)
30            }
31            Error::Io(e) => write!(f, "I/O error: {}", e),
32            Error::Timeout => write!(f, "timeout"),
33            Error::Xml(e) => write!(f, "(de)serialization error: {}", e),
34            Error::Other(e) => write!(f, "error: {}", e),
35        }
36    }
37}
38
39impl From<Box<dyn StdError + Send + Sync>> for Error {
40    fn from(e: Box<dyn StdError + Send + Sync>) -> Self {
41        Self::Other(e)
42    }
43}
44
45impl From<io::Error> for Error {
46    fn from(e: io::Error) -> Self {
47        Self::Io(e)
48    }
49}
50
51impl From<io::ErrorKind> for Error {
52    fn from(e: io::ErrorKind) -> Self {
53        Self::Io(io::Error::from(e))
54    }
55}
56
57impl From<TryFromIntError> for Error {
58    fn from(e: TryFromIntError) -> Self {
59        Self::Other(e.into())
60    }
61}
62
63impl From<FromUtf8Error> for Error {
64    fn from(e: FromUtf8Error) -> Self {
65        Self::Other(e.into())
66    }
67}
68
69impl From<Utf8Error> for Error {
70    fn from(e: Utf8Error) -> Self {
71        Self::Other(e.into())
72    }
73}
74
75impl From<TryFromSliceError> for Error {
76    fn from(e: TryFromSliceError) -> Self {
77        Self::Other(e.into())
78    }
79}