error.rs

  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 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    /// Connection closed
 30    Disconnected,
 31    /// Shoud never happen
 32    InvalidState,
 33}
 34
 35/// Causes for stream parsing errors
 36#[derive(Debug, Error)]
 37pub enum ParserError {
 38    /// Encoding error
 39    Utf8(Utf8Error),
 40    /// XML parse error
 41    Parse(ParseError),
 42    /// Illegal `</>`
 43    ShortTag,
 44    /// Required by `impl Decoder`
 45    IO(IoError),
 46}
 47
 48impl From<ParserError> for Error {
 49    fn from(e: ParserError) -> Self {
 50        ProtocolError::Parser(e).into()
 51    }
 52}
 53
 54/// XML parse error wrapper type
 55#[derive(Debug)]
 56pub struct ParseError(pub Cow<'static, str>);
 57
 58impl StdError for ParseError {
 59    fn description(&self) -> &str {
 60        self.0.as_ref()
 61    }
 62    fn cause(&self) -> Option<&StdError> {
 63        None
 64    }
 65}
 66
 67impl fmt::Display for ParseError {
 68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 69        write!(f, "{}", self.0)
 70    }
 71}
 72
 73/// XMPP protocol-level error
 74#[derive(Debug, Error)]
 75pub enum ProtocolError {
 76    /// XML parser error
 77    Parser(ParserError),
 78    /// Error with expected stanza schema
 79    #[error(non_std)]
 80    Parsers(ParsersError),
 81    /// No TLS available
 82    NoTls,
 83    /// Invalid response to resource binding
 84    InvalidBindResponse,
 85    /// No xmlns attribute in <stream:stream>
 86    NoStreamNamespace,
 87    /// No id attribute in <stream:stream>
 88    NoStreamId,
 89    /// Encountered an unexpected XML token
 90    InvalidToken,
 91}
 92
 93/// Authentication error
 94#[derive(Debug, Error)]
 95pub enum AuthError {
 96    /// No matching SASL mechanism available
 97    NoMechanism,
 98    /// Local SASL implementation error
 99    #[error(no_from, non_std, msg_embedded)]
100    Sasl(String),
101    /// Failure from server
102    #[error(non_std)]
103    Fail(SaslDefinedCondition),
104    /// Component authentication failure
105    #[error(no_from)]
106    ComponentFail,
107}
108
109/// Error establishing connection
110#[derive(Debug)]
111pub enum ConnecterError {
112    /// All attempts failed, no error available
113    AllFailed,
114    /// DNS protocol error
115    Dns(ProtoError),
116    /// DNS resolution error
117    Resolve(ResolveError),
118}
119
120impl std::error::Error for ConnecterError {}
121
122impl std::fmt::Display for ConnecterError {
123    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
124        write!(fmt, "{:?}", self)
125    }
126}