tokio-xmpp: Error out when the stream:features couldn’t be parsed

This is nicer than sleeping forever with no debug info whatsoever.
This commit is contained in:
Emmanuel Gil Peyrot 2024-08-03 15:52:15 +02:00
parent ba6a7c4744
commit 35932268af

View file

@ -1,7 +1,7 @@
use futures::{sink::SinkExt, stream::StreamExt}; use futures::{sink::SinkExt, stream::StreamExt};
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed; use tokio_util::codec::Framed;
use xmpp_parsers::{jid::Jid, ns, stream_features::StreamFeatures}; use xmpp_parsers::{jid::Jid, ns, stream_features::StreamFeatures, Error as ParsersError};
use crate::error::{Error, ProtocolError}; use crate::error::{Error, ProtocolError};
use crate::xmpp_codec::{Packet, XmppCodec}; use crate::xmpp_codec::{Packet, XmppCodec};
@ -50,10 +50,10 @@ pub async fn start<S: AsyncRead + AsyncWrite + Unpin>(
loop { loop {
match stream.next().await { match stream.next().await {
Some(Ok(Packet::Stanza(stanza))) => { Some(Ok(Packet::Stanza(stanza))) => {
if let Ok(stream_features) = StreamFeatures::try_from(stanza) { let stream_features = StreamFeatures::try_from(stanza)
.map_err(|e| Error::Protocol(ParsersError::from(e).into()))?;
return Ok(XMPPStream::new(jid, stream, ns, stream_id, stream_features)); return Ok(XMPPStream::new(jid, stream, ns, stream_id, stream_features));
} }
}
Some(Ok(_)) => {} Some(Ok(_)) => {}
Some(Err(e)) => return Err(e.into()), Some(Err(e)) => return Err(e.into()),
None => return Err(Error::Disconnected), None => return Err(Error::Disconnected),