xmpp-rs-mirror/src/error.rs

117 lines
2.7 KiB
Rust
Raw Normal View History

2018-09-06 15:46:06 +00:00
use std::io::Error as IoError;
use std::error::Error as StdError;
use std::str::Utf8Error;
use std::borrow::Cow;
use std::fmt;
use native_tls::Error as TlsError;
2018-09-06 16:20:05 +00:00
use trust_dns_resolver::error::ResolveError;
use trust_dns_proto::error::ProtoError;
2018-09-06 15:46:06 +00:00
use xmpp_parsers::error::Error as ParsersError;
use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
2018-09-07 23:42:23 +00:00
/// Top-level error type
2018-09-06 15:46:06 +00:00
#[derive(Debug, Error)]
pub enum Error {
2018-09-07 23:42:23 +00:00
/// I/O error
2018-09-06 15:46:06 +00:00
Io(IoError),
2018-09-07 23:42:23 +00:00
/// Error resolving DNS and establishing a connection
2018-09-06 15:46:06 +00:00
Connection(ConnecterError),
/// DNS label conversion error, no details available from module
/// `idna`
Idna,
2018-09-07 23:42:23 +00:00
/// Protocol-level error
2018-09-06 15:46:06 +00:00
Protocol(ProtocolError),
2018-09-07 23:42:23 +00:00
/// Authentication error
2018-09-06 15:46:06 +00:00
Auth(AuthError),
2018-09-07 23:42:23 +00:00
/// TLS error
2018-09-06 15:46:06 +00:00
Tls(TlsError),
/// Shoud never happen
InvalidState,
}
/// Causes for stream parsing errors
#[derive(Debug, Error)]
pub enum ParserError {
/// Encoding error
Utf8(Utf8Error),
/// XML parse error
Parse(ParseError),
/// Illegal `</>`
ShortTag,
/// Required by `impl Decoder`
IO(IoError),
}
impl From<ParserError> for Error {
fn from(e: ParserError) -> Self {
ProtocolError::Parser(e).into()
}
}
/// XML parse error wrapper type
#[derive(Debug)]
pub struct ParseError(pub Cow<'static, str>);
impl StdError for ParseError {
fn description(&self) -> &str {
self.0.as_ref()
}
fn cause(&self) -> Option<&StdError> {
None
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2018-09-07 23:42:23 +00:00
/// XMPP protocol-level error
2018-09-06 15:46:06 +00:00
#[derive(Debug, Error)]
pub enum ProtocolError {
2018-09-07 23:42:23 +00:00
/// XML parser error
2018-09-06 15:46:06 +00:00
Parser(ParserError),
2018-09-07 23:42:23 +00:00
/// Error with expected stanza schema
2018-09-06 15:46:06 +00:00
#[error(non_std)]
Parsers(ParsersError),
2018-09-07 23:42:23 +00:00
/// No TLS available
2018-09-06 15:46:06 +00:00
NoTls,
2018-09-07 23:42:23 +00:00
/// Invalid response to resource binding
2018-09-06 15:46:06 +00:00
InvalidBindResponse,
2018-09-07 23:42:23 +00:00
/// No xmlns attribute in <stream:stream>
2018-09-06 15:46:06 +00:00
NoStreamNamespace,
2018-09-07 23:42:23 +00:00
/// No id attribute in <stream:stream>
2018-09-06 15:46:06 +00:00
NoStreamId,
2018-09-07 23:42:23 +00:00
/// Encountered an unexpected XML token
2018-09-06 15:46:06 +00:00
InvalidToken,
}
2018-09-07 23:42:23 +00:00
/// Authentication error
2018-09-06 15:46:06 +00:00
#[derive(Debug, Error)]
pub enum AuthError {
2018-09-07 23:42:23 +00:00
/// No matching SASL mechanism available
2018-09-06 15:46:06 +00:00
NoMechanism,
2018-09-07 23:42:23 +00:00
/// Local SASL implementation error
2018-09-06 15:46:06 +00:00
#[error(no_from, non_std, msg_embedded)]
Sasl(String),
2018-09-07 23:42:23 +00:00
/// Failure from server
2018-09-06 15:46:06 +00:00
#[error(non_std)]
Fail(SaslDefinedCondition),
2018-09-07 23:42:23 +00:00
/// Component authentication failure
2018-09-06 15:46:06 +00:00
#[error(no_from)]
ComponentFail,
}
2018-09-07 23:42:23 +00:00
/// Error establishing connection
2018-09-06 15:46:06 +00:00
#[derive(Debug, Error)]
pub enum ConnecterError {
2018-09-07 23:42:23 +00:00
/// All attempts failed, no error available
2018-09-06 15:46:06 +00:00
AllFailed,
2018-09-07 23:42:23 +00:00
/// DNS protocol error
2018-09-06 16:20:05 +00:00
Dns(ProtoError),
/// DNS resolution error
Resolve(ResolveError),
}