error.rs

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