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

132 lines
3.9 KiB
Rust
Raw Normal View History

use core::{fmt, net::AddrParseError, str::Utf8Error};
#[cfg(feature = "dns")]
2026-05-14 20:45:37 +02:00
use hickory_resolver::{net::NetError as DnsNetError, proto::ProtoError as DnsProtoError};
2020-12-26 20:11:23 +01:00
use sasl::client::MechanismError as SaslMechanismError;
use std::io;
use thiserror::Error;
2018-09-06 18:20:05 +02:00
use xmpp_parsers::stream_error::ReceivedStreamError;
use crate::{
connect::ServerConnectorError, jid, minidom,
parsers::sasl::DefinedCondition as SaslDefinedCondition, xmlstream::RecvFeaturesError,
};
2018-09-08 01:42:23 +02:00
/// Top-level error type
#[derive(Debug, Error)]
2018-09-06 17:46:06 +02:00
pub enum Error {
2018-09-08 01:42:23 +02:00
/// I/O error
#[error("I/O error: {0}")]
Io(#[from] io::Error),
2020-03-05 01:25:24 +01:00
/// Error parsing Jabber-Id
#[error("JID parse error: {0}")]
JidParse(#[from] jid::Error),
2018-09-08 01:42:23 +02:00
/// Protocol-level error
#[error("protocol error: {0}")]
Protocol(#[from] ProtocolError),
2018-09-08 01:42:23 +02:00
/// Authentication error
#[error("authentication error: {0}")]
Auth(#[from] AuthError),
2018-12-20 20:39:01 +01:00
/// Connection closed
#[error("disconnected")]
2018-12-20 20:39:01 +01:00
Disconnected,
/// Should never happen
#[error("invalid state")]
2018-09-06 17:46:06 +02:00
InvalidState,
/// Fmt error
#[error("fmt error: {0}")]
Fmt(#[from] fmt::Error),
/// Utf8 error
#[error("UTF-8 error: {0}")]
Utf8(#[from] Utf8Error),
/// Error specific to ServerConnector impl
#[error("connection error: {0}")]
Connection(Box<dyn ServerConnectorError>),
/// DNS protocol error
#[cfg(feature = "dns")]
#[error("{0:?}")]
2026-05-14 20:45:37 +02:00
DnsProto(#[from] DnsProtoError),
/// DNS network error
#[cfg(feature = "dns")]
#[error("{0:?}")]
2026-05-14 20:45:37 +02:00
DnsNet(#[from] DnsNetError),
/// DNS label conversion error, no details available from module
/// `idna`
#[cfg(feature = "dns")]
#[error("IDNA error")]
Idna,
/// Invalid IP/Port address
#[error("wrong network address: {0}")]
Addr(#[from] AddrParseError),
/// Received a stream error
#[error("{0}")]
StreamError(ReceivedStreamError),
2018-09-06 17:46:06 +02:00
}
impl<T: ServerConnectorError + 'static> From<T> for Error {
fn from(e: T) -> Self {
Error::Connection(Box::new(e))
}
}
#[cfg(feature = "dns")]
impl From<idna::Errors> for Error {
fn from(_e: idna::Errors) -> Self {
Error::Idna
}
}
impl From<RecvFeaturesError> for Error {
fn from(e: RecvFeaturesError) -> Self {
match e {
RecvFeaturesError::Io(e) => e.into(),
RecvFeaturesError::StreamError(e) => Self::StreamError(e),
}
}
}
2018-09-08 01:42:23 +02:00
/// XMPP protocol-level error
#[derive(Debug, Error)]
2018-09-06 17:46:06 +02:00
pub enum ProtocolError {
2018-09-08 01:42:23 +02:00
/// XML parser error
#[error("XML parser error: {0}")]
Parser(#[from] minidom::Error),
2018-09-08 01:42:23 +02:00
/// Error with expected stanza schema
#[error("error with expected stanza schema: {0}")]
Parsers(#[from] xso::error::Error),
2018-09-08 01:42:23 +02:00
/// No TLS available
#[error("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
#[error("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>
#[error("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>
#[error("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
#[error("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)
#[error("unexpected <stream:stream>")]
2019-01-26 21:07:15 +01:00
InvalidStreamStart,
2018-09-06 17:46:06 +02:00
}
2018-09-08 01:42:23 +02:00
/// Authentication error
#[derive(Debug, Error)]
2018-09-06 17:46:06 +02:00
pub enum AuthError {
2018-09-08 01:42:23 +02:00
/// No matching SASL mechanism available
#[error("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
#[error("local SASL implementation error: {0}")]
2020-12-26 20:11:23 +01:00
Sasl(SaslMechanismError),
2018-09-08 01:42:23 +02:00
/// Failure from server
#[error("failure from the server: {0:?}")]
2018-09-06 17:46:06 +02:00
Fail(SaslDefinedCondition),
2018-09-08 01:42:23 +02:00
/// Component authentication failure
#[error("component authentication failure")]
2018-09-06 17:46:06 +02:00
ComponentFail,
}