1use native_tls::Error as TlsError;
2use std::borrow::Cow;
3use std::error::Error as StdError;
4use std::fmt;
5use std::io::Error as IoError;
6use std::str::Utf8Error;
7use trust_dns_proto::error::ProtoError;
8use trust_dns_resolver::error::ResolveError;
9
10use xmpp_parsers::error::Error as ParsersError;
11use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
12
13/// Top-level error type
14#[derive(Debug, Error)]
15pub enum Error {
16 /// I/O error
17 Io(IoError),
18 /// Error resolving DNS and establishing a connection
19 Connection(ConnecterError),
20 /// DNS label conversion error, no details available from module
21 /// `idna`
22 Idna,
23 /// Protocol-level error
24 Protocol(ProtocolError),
25 /// Authentication error
26 Auth(AuthError),
27 /// TLS error
28 Tls(TlsError),
29 /// Shoud never happen
30 InvalidState,
31}
32
33/// Causes for stream parsing errors
34#[derive(Debug, Error)]
35pub enum ParserError {
36 /// Encoding error
37 Utf8(Utf8Error),
38 /// XML parse error
39 Parse(ParseError),
40 /// Illegal `</>`
41 ShortTag,
42 /// Required by `impl Decoder`
43 IO(IoError),
44}
45
46impl From<ParserError> for Error {
47 fn from(e: ParserError) -> Self {
48 ProtocolError::Parser(e).into()
49 }
50}
51
52/// XML parse error wrapper type
53#[derive(Debug)]
54pub struct ParseError(pub Cow<'static, str>);
55
56impl StdError for ParseError {
57 fn description(&self) -> &str {
58 self.0.as_ref()
59 }
60 fn cause(&self) -> Option<&StdError> {
61 None
62 }
63}
64
65impl fmt::Display for ParseError {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 write!(f, "{}", self.0)
68 }
69}
70
71/// XMPP protocol-level error
72#[derive(Debug, Error)]
73pub enum ProtocolError {
74 /// XML parser error
75 Parser(ParserError),
76 /// Error with expected stanza schema
77 #[error(non_std)]
78 Parsers(ParsersError),
79 /// No TLS available
80 NoTls,
81 /// Invalid response to resource binding
82 InvalidBindResponse,
83 /// No xmlns attribute in <stream:stream>
84 NoStreamNamespace,
85 /// No id attribute in <stream:stream>
86 NoStreamId,
87 /// Encountered an unexpected XML token
88 InvalidToken,
89}
90
91/// Authentication error
92#[derive(Debug, Error)]
93pub enum AuthError {
94 /// No matching SASL mechanism available
95 NoMechanism,
96 /// Local SASL implementation error
97 #[error(no_from, non_std, msg_embedded)]
98 Sasl(String),
99 /// Failure from server
100 #[error(non_std)]
101 Fail(SaslDefinedCondition),
102 /// Component authentication failure
103 #[error(no_from)]
104 ComponentFail,
105}
106
107/// Error establishing connection
108#[derive(Debug, Error)]
109pub enum ConnecterError {
110 /// All attempts failed, no error available
111 AllFailed,
112 /// DNS protocol error
113 Dns(ProtoError),
114 /// DNS resolution error
115 Resolve(ResolveError),
116}