Skip to content

Commit 1c63ae0

Browse files
introduce client::RecvError
1 parent d462a9e commit 1c63ae0

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

iroh-relay/src/client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::warn;
2020
use tracing::{debug, event, trace, Level};
2121
use url::Url;
2222

23-
pub use self::conn::{ConnSendError, HandshakeError, ReceivedMessage, SendMessage};
23+
pub use self::conn::{ConnSendError, HandshakeError, ReceivedMessage, RecvError, SendMessage};
2424
#[cfg(not(wasm_browser))]
2525
use crate::dns::{DnsResolver, Error as DnsError};
2626
use crate::{
@@ -110,8 +110,6 @@ pub enum Error {
110110
Timeout(#[from] time::Elapsed),
111111
#[error(transparent)]
112112
Http(#[from] hyper::http::Error),
113-
#[error("Unexpected frame received {0}")]
114-
UnexpectedFrame(crate::protos::relay::FrameType),
115113
#[error(transparent)]
116114
Utf8(#[from] std::str::Utf8Error),
117115
#[cfg(wasm_browser)]
@@ -298,7 +296,7 @@ impl Client {
298296
}
299297

300298
impl Stream for Client {
301-
type Item = Result<ReceivedMessage, Error>;
299+
type Item = Result<ReceivedMessage, RecvError>;
302300

303301
fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
304302
Pin::new(&mut self.conn).poll_next(cx)
@@ -384,7 +382,7 @@ impl ClientStream {
384382
}
385383

386384
impl Stream for ClientStream {
387-
type Item = Result<ReceivedMessage, Error>;
385+
type Item = Result<ReceivedMessage, RecvError>;
388386

389387
fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
390388
Pin::new(&mut self.stream).poll_next(cx)

iroh-relay/src/client/conn.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tokio_tungstenite_wasm::WebSocketStream;
1616
use tokio_util::codec::Framed;
1717
use tracing::debug;
1818

19-
use super::{ConnectError, Error, KeyCache};
19+
use super::{ConnectError, KeyCache};
2020
use crate::protos::{
2121
self,
2222
relay::{ClientInfo, Frame, MAX_PACKET_SIZE, PROTOCOL_VERSION},
@@ -28,7 +28,7 @@ use crate::{client::streams::MaybeTlsStreamChained, protos::relay::RelayCodec};
2828
#[derive(Debug, thiserror::Error)]
2929
pub enum ConnSendError {
3030
/// An IO error.
31-
#[error("IO error")]
31+
#[error(transparent)]
3232
Io(#[from] io::Error),
3333
/// A protocol error.
3434
#[error("Protocol error")]
@@ -43,6 +43,23 @@ pub enum HandshakeError {
4343
Send(#[from] protos::relay::Error),
4444
}
4545

46+
/// Errors when receiving messages from the relay server.
47+
#[derive(Debug, thiserror::Error)]
48+
#[allow(missing_docs)]
49+
pub enum RecvError {
50+
/// An IO error.
51+
#[error(transparent)]
52+
Io(#[from] io::Error),
53+
#[error(transparent)]
54+
Protocol(#[from] crate::protos::relay::Error),
55+
#[error(transparent)]
56+
Websocket(#[from] tokio_tungstenite_wasm::Error),
57+
#[error("invalid protocol message encoding")]
58+
InvalidProtocolMessageEncoding,
59+
#[error("Unexpected frame received {0}")]
60+
UnexpectedFrame(crate::protos::relay::FrameType),
61+
}
62+
4663
impl From<tokio_tungstenite_wasm::Error> for ConnSendError {
4764
fn from(source: tokio_tungstenite_wasm::Error) -> Self {
4865
let io_err = match source {
@@ -125,7 +142,7 @@ async fn server_handshake(writer: &mut Conn, secret_key: &SecretKey) -> Result<(
125142
}
126143

127144
impl Stream for Conn {
128-
type Item = Result<ReceivedMessage, Error>;
145+
type Item = Result<ReceivedMessage, RecvError>;
129146

130147
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
131148
match *self {
@@ -303,7 +320,7 @@ pub enum ReceivedMessage {
303320
}
304321

305322
impl TryFrom<Frame> for ReceivedMessage {
306-
type Error = Error;
323+
type Error = RecvError;
307324

308325
fn try_from(frame: Frame) -> std::result::Result<Self, Self::Error> {
309326
match frame {
@@ -323,7 +340,9 @@ impl TryFrom<Frame> for ReceivedMessage {
323340
Frame::Ping { data } => Ok(ReceivedMessage::Ping(data)),
324341
Frame::Pong { data } => Ok(ReceivedMessage::Pong(data)),
325342
Frame::Health { problem } => {
326-
let problem = std::str::from_utf8(&problem)?.to_owned();
343+
let problem = std::str::from_utf8(&problem)
344+
.map_err(|_| RecvError::InvalidProtocolMessageEncoding)?
345+
.to_owned();
327346
let problem = Some(problem);
328347
Ok(ReceivedMessage::Health { problem })
329348
}
@@ -338,7 +357,7 @@ impl TryFrom<Frame> for ReceivedMessage {
338357
try_for,
339358
})
340359
}
341-
_ => Err(Error::UnexpectedFrame(frame.typ())),
360+
_ => Err(RecvError::UnexpectedFrame(frame.typ())),
342361
}
343362
}
344363
}

iroh-relay/src/server/http_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ mod tests {
821821
}
822822

823823
fn process_msg(
824-
msg: Option<Result<ReceivedMessage, crate::client::Error>>,
824+
msg: Option<Result<ReceivedMessage, crate::client::RecvError>>,
825825
) -> Option<(PublicKey, Bytes)> {
826826
match msg {
827827
Some(Err(e)) => {

0 commit comments

Comments
 (0)