xmpp-rs/tokio-xmpp/src/error.rs

230 lines
6.4 KiB
Rust
Raw Normal View History

#[cfg(feature = "dns")]
use hickory_resolver::{
error::ResolveError as DnsResolveError, proto::error::ProtoError as DnsProtoError,
};
2020-12-26 20:11:23 +01:00
use sasl::client::MechanismError as SaslMechanismError;
2018-12-18 19:04:31 +01:00
use std::error::Error as StdError;
2018-09-06 17:46:06 +02:00
use std::fmt;
2018-12-18 19:04:31 +01:00
use std::io::Error as IoError;
use std::net::AddrParseError;
use std::str::Utf8Error;
2018-09-06 18:20:05 +02:00
2018-09-06 17:46:06 +02:00
use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
use xmpp_parsers::{jid::Error as JidParseError, Error as ParsersError};
2018-09-06 17:46:06 +02:00
use crate::connect::ServerConnectorError;
2018-09-08 01:42:23 +02:00
/// Top-level error type
#[derive(Debug)]
2018-09-06 17:46:06 +02:00
pub enum Error {
2018-09-08 01:42:23 +02:00
/// I/O error
2018-09-06 17:46:06 +02:00
Io(IoError),
2020-03-05 01:25:24 +01:00
/// Error parsing Jabber-Id
JidParse(JidParseError),
2018-09-08 01:42:23 +02:00
/// Protocol-level error
2018-09-06 17:46:06 +02:00
Protocol(ProtocolError),
2018-09-08 01:42:23 +02:00
/// Authentication error
2018-09-06 17:46:06 +02:00
Auth(AuthError),
2018-12-20 20:39:01 +01:00
/// Connection closed
Disconnected,
/// Should never happen
2018-09-06 17:46:06 +02:00
InvalidState,
/// Fmt error
Fmt(fmt::Error),
/// Utf8 error
Utf8(Utf8Error),
/// Error specific to ServerConnector impl
Connection(Box<dyn ServerConnectorError>),
/// DNS protocol error
#[cfg(feature = "dns")]
Dns(DnsProtoError),
/// DNS resolution error
#[cfg(feature = "dns")]
Resolve(DnsResolveError),
/// DNS label conversion error, no details available from module
/// `idna`
#[cfg(feature = "dns")]
Idna,
/// Invalid IP/Port address
Addr(AddrParseError),
2018-09-06 17:46:06 +02:00
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(e) => write!(fmt, "IO error: {}", e),
Error::Connection(e) => write!(fmt, "connection error: {}", e),
2020-03-05 01:25:24 +01:00
Error::JidParse(e) => write!(fmt, "jid parse error: {}", e),
Error::Protocol(e) => write!(fmt, "protocol error: {}", e),
Error::Auth(e) => write!(fmt, "authentication error: {}", e),
Error::Disconnected => write!(fmt, "disconnected"),
Error::InvalidState => write!(fmt, "invalid state"),
Error::Fmt(e) => write!(fmt, "Fmt error: {}", e),
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
#[cfg(feature = "dns")]
Error::Dns(e) => write!(fmt, "{:?}", e),
#[cfg(feature = "dns")]
Error::Resolve(e) => write!(fmt, "{:?}", e),
#[cfg(feature = "dns")]
Error::Idna => write!(fmt, "IDNA error"),
Error::Addr(e) => write!(fmt, "Wrong network address: {e}"),
}
}
}
impl StdError for Error {}
impl From<IoError> for Error {
fn from(e: IoError) -> Self {
Error::Io(e)
}
}
impl<T: ServerConnectorError + 'static> From<T> for Error {
fn from(e: T) -> Self {
Error::Connection(Box::new(e))
}
}
2020-03-05 01:25:24 +01:00
impl From<JidParseError> for Error {
fn from(e: JidParseError) -> Self {
Error::JidParse(e)
}
}
impl From<ProtocolError> for Error {
fn from(e: ProtocolError) -> Self {
Error::Protocol(e)
}
}
impl From<AuthError> for Error {
fn from(e: AuthError) -> Self {
Error::Auth(e)
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Self {
Error::Fmt(e)
}
}
impl From<Utf8Error> for Error {
fn from(e: Utf8Error) -> Self {
Error::Utf8(e)
}
}
#[cfg(feature = "dns")]
impl From<idna::Errors> for Error {
fn from(_e: idna::Errors) -> Self {
Error::Idna
}
}
#[cfg(feature = "dns")]
impl From<DnsResolveError> for Error {
fn from(e: DnsResolveError) -> Error {
Error::Resolve(e)
}
}
#[cfg(feature = "dns")]
impl From<DnsProtoError> for Error {
fn from(e: DnsProtoError) -> Error {
Error::Dns(e)
}
}
impl From<AddrParseError> for Error {
fn from(e: AddrParseError) -> Error {
Error::Addr(e)
}
}
2018-09-08 01:42:23 +02:00
/// XMPP protocol-level error
#[derive(Debug)]
2018-09-06 17:46:06 +02:00
pub enum ProtocolError {
2018-09-08 01:42:23 +02:00
/// XML parser error
Parser(minidom::Error),
2018-09-08 01:42:23 +02:00
/// Error with expected stanza schema
2018-09-06 17:46:06 +02:00
Parsers(ParsersError),
2018-09-08 01:42:23 +02:00
/// No TLS available
2018-09-06 17:46:06 +02:00
NoTls,
2018-09-08 01:42:23 +02:00
/// Invalid response to resource binding
2018-09-06 17:46:06 +02:00
InvalidBindResponse,
2018-09-08 01:42:23 +02:00
/// No xmlns attribute in <stream:stream>
2018-09-06 17:46:06 +02:00
NoStreamNamespace,
2018-09-08 01:42:23 +02:00
/// No id attribute in <stream:stream>
2018-09-06 17:46:06 +02:00
NoStreamId,
2018-09-08 01:42:23 +02:00
/// Encountered an unexpected XML token
2018-09-06 17:46:06 +02:00
InvalidToken,
2019-01-26 21:07:15 +01:00
/// Unexpected <stream:stream> (shouldn't occur)
InvalidStreamStart,
2018-09-06 17:46:06 +02:00
}
impl fmt::Display for ProtocolError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
ProtocolError::Parser(e) => write!(fmt, "XML parser error: {}", e),
ProtocolError::Parsers(e) => write!(fmt, "error with expected stanza schema: {}", e),
ProtocolError::NoTls => write!(fmt, "no TLS available"),
ProtocolError::InvalidBindResponse => {
write!(fmt, "invalid response to resource binding")
}
ProtocolError::NoStreamNamespace => {
write!(fmt, "no xmlns attribute in <stream:stream>")
}
ProtocolError::NoStreamId => write!(fmt, "no id attribute in <stream:stream>"),
ProtocolError::InvalidToken => write!(fmt, "encountered an unexpected XML token"),
ProtocolError::InvalidStreamStart => write!(fmt, "unexpected <stream:stream>"),
}
}
}
impl StdError for ProtocolError {}
impl From<minidom::Error> for ProtocolError {
fn from(e: minidom::Error) -> Self {
ProtocolError::Parser(e)
}
}
impl From<minidom::Error> for Error {
fn from(e: minidom::Error) -> Self {
ProtocolError::Parser(e).into()
}
}
impl From<ParsersError> for ProtocolError {
fn from(e: ParsersError) -> Self {
ProtocolError::Parsers(e)
}
}
2018-09-08 01:42:23 +02:00
/// Authentication error
#[derive(Debug)]
2018-09-06 17:46:06 +02:00
pub enum AuthError {
2018-09-08 01:42:23 +02:00
/// No matching SASL mechanism available
2018-09-06 17:46:06 +02:00
NoMechanism,
2018-09-08 01:42:23 +02:00
/// Local SASL implementation error
2020-12-26 20:11:23 +01:00
Sasl(SaslMechanismError),
2018-09-08 01:42:23 +02:00
/// Failure from server
2018-09-06 17:46:06 +02:00
Fail(SaslDefinedCondition),
2018-09-08 01:42:23 +02:00
/// Component authentication failure
2018-09-06 17:46:06 +02:00
ComponentFail,
}
impl StdError for AuthError {}
impl fmt::Display for AuthError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
AuthError::NoMechanism => write!(fmt, "no matching SASL mechanism available"),
AuthError::Sasl(s) => write!(fmt, "local SASL implementation error: {}", s),
AuthError::Fail(c) => write!(fmt, "failure from the server: {:?}", c),
AuthError::ComponentFail => write!(fmt, "component authentication failure"),
}
}
}