1use 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#[derive(Debug)]
15#[non_exhaustive]
16pub enum Error {
17 Command(Box<ResponseStatus>),
18 Io(std::io::Error),
19 Timeout,
20 Xml(Box<dyn StdError + Send + Sync>),
21 Other(Box<dyn StdError + Send + Sync>),
22 ResponseTooLarge { expected: usize, max: usize },
23}
24
25impl StdError for Error {}
26
27impl Display for Error {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 Self::Command(e) => {
31 write!(f, "command error: {}", e.result.message)
32 }
33 Self::Io(e) => write!(f, "I/O error: {e}"),
34 Self::Timeout => write!(f, "timeout"),
35 Self::Xml(e) => write!(f, "(de)serialization error: {e}"),
36 Self::Other(e) => write!(f, "error: {e}"),
37 Self::ResponseTooLarge { expected, max } => {
38 write!(f, "response too large: expected {expected}, max {max}")
39 }
40 }
41 }
42}
43
44impl From<Box<dyn StdError + Send + Sync>> for Error {
45 fn from(e: Box<dyn StdError + Send + Sync>) -> Self {
46 Self::Other(e)
47 }
48}
49
50impl From<io::Error> for Error {
51 fn from(e: io::Error) -> Self {
52 Self::Io(e)
53 }
54}
55
56impl From<io::ErrorKind> for Error {
57 fn from(e: io::ErrorKind) -> Self {
58 Self::Io(io::Error::from(e))
59 }
60}
61
62impl From<TryFromIntError> for Error {
63 fn from(e: TryFromIntError) -> Self {
64 Self::Other(e.into())
65 }
66}
67
68impl From<FromUtf8Error> for Error {
69 fn from(e: FromUtf8Error) -> Self {
70 Self::Other(e.into())
71 }
72}
73
74impl From<Utf8Error> for Error {
75 fn from(e: Utf8Error) -> Self {
76 Self::Other(e.into())
77 }
78}
79
80impl From<TryFromSliceError> for Error {
81 fn from(e: TryFromSliceError) -> Self {
82 Self::Other(e.into())
83 }
84}