@@ -16,7 +16,7 @@ use tokio_tungstenite_wasm::WebSocketStream;
16
16
use tokio_util:: codec:: Framed ;
17
17
use tracing:: debug;
18
18
19
- use super :: { ConnectError , Error , KeyCache } ;
19
+ use super :: { ConnectError , KeyCache } ;
20
20
use crate :: protos:: {
21
21
self ,
22
22
relay:: { ClientInfo , Frame , MAX_PACKET_SIZE , PROTOCOL_VERSION } ,
@@ -28,7 +28,7 @@ use crate::{client::streams::MaybeTlsStreamChained, protos::relay::RelayCodec};
28
28
#[ derive( Debug , thiserror:: Error ) ]
29
29
pub enum ConnSendError {
30
30
/// An IO error.
31
- #[ error( "IO error" ) ]
31
+ #[ error( transparent ) ]
32
32
Io ( #[ from] io:: Error ) ,
33
33
/// A protocol error.
34
34
#[ error( "Protocol error" ) ]
@@ -43,6 +43,23 @@ pub enum HandshakeError {
43
43
Send ( #[ from] protos:: relay:: Error ) ,
44
44
}
45
45
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
+
46
63
impl From < tokio_tungstenite_wasm:: Error > for ConnSendError {
47
64
fn from ( source : tokio_tungstenite_wasm:: Error ) -> Self {
48
65
let io_err = match source {
@@ -125,7 +142,7 @@ async fn server_handshake(writer: &mut Conn, secret_key: &SecretKey) -> Result<(
125
142
}
126
143
127
144
impl Stream for Conn {
128
- type Item = Result < ReceivedMessage , Error > ;
145
+ type Item = Result < ReceivedMessage , RecvError > ;
129
146
130
147
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
131
148
match * self {
@@ -303,7 +320,7 @@ pub enum ReceivedMessage {
303
320
}
304
321
305
322
impl TryFrom < Frame > for ReceivedMessage {
306
- type Error = Error ;
323
+ type Error = RecvError ;
307
324
308
325
fn try_from ( frame : Frame ) -> std:: result:: Result < Self , Self :: Error > {
309
326
match frame {
@@ -323,7 +340,9 @@ impl TryFrom<Frame> for ReceivedMessage {
323
340
Frame :: Ping { data } => Ok ( ReceivedMessage :: Ping ( data) ) ,
324
341
Frame :: Pong { data } => Ok ( ReceivedMessage :: Pong ( data) ) ,
325
342
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 ( ) ;
327
346
let problem = Some ( problem) ;
328
347
Ok ( ReceivedMessage :: Health { problem } )
329
348
}
@@ -338,7 +357,7 @@ impl TryFrom<Frame> for ReceivedMessage {
338
357
try_for,
339
358
} )
340
359
}
341
- _ => Err ( Error :: UnexpectedFrame ( frame. typ ( ) ) ) ,
360
+ _ => Err ( RecvError :: UnexpectedFrame ( frame. typ ( ) ) ) ,
342
361
}
343
362
}
344
363
}
0 commit comments