diff --git a/matchbox_socket/src/error.rs b/matchbox_socket/src/error.rs index 9571fd93..de3f467e 100644 --- a/matchbox_socket/src/error.rs +++ b/matchbox_socket/src/error.rs @@ -5,11 +5,20 @@ use crate::webrtc_socket::error::SignalingError; pub enum Error { /// An error occurring if the connection fails to establish. Perhaps check your connection or try again. #[error("The connection failed to establish. Check your connection and try again.")] - ConnectionFailed, + ConnectionFailed { + /// The source of the connection failure + source: SignalingError, + }, /// Unexpected fatal error ocurred with messaging. Please file an issue or triage. - #[error("An unexpected error ocurred with messaging: {0}")] - Communication(#[from] SignalingError), + #[error("An unexpected error ocurred at runtime with messaging: {source}")] + Runtime { + /// The source of the connection failure + source: SignalingError, + }, /// Kicked by the server or disconnected #[error("The signaling server connection was severed.")] - Disconnected, + Disconnected { + /// The source of the connection failure + source: SignalingError, + }, } diff --git a/matchbox_socket/src/webrtc_socket/error.rs b/matchbox_socket/src/webrtc_socket/error.rs index f870cb71..255a04b9 100644 --- a/matchbox_socket/src/webrtc_socket/error.rs +++ b/matchbox_socket/src/webrtc_socket/error.rs @@ -34,7 +34,7 @@ pub enum SignalingError { UnknownFormat, #[error("failed to establish initial connection: {0}")] - ConnectionFailed(#[from] Box), + NegotiationFailed(#[from] Box), // Native #[cfg(not(target_arch = "wasm32"))] @@ -46,11 +46,11 @@ pub enum SignalingError { #[error("socket failure communicating with signaling server: {0}")] Socket(#[from] ws_stream_wasm::WsErr), - #[error("failed to send message to peer")] + #[error("failed to send message to peer over javascript: {0}")] #[cfg(target_arch = "wasm32")] JsPacket(#[from] JsError), - #[error("failed to send message to peer")] + #[error("failed to send message to peer: {0}")] Packet(#[from] SendError), } diff --git a/matchbox_socket/src/webrtc_socket/native.rs b/matchbox_socket/src/webrtc_socket/native.rs index c21cbcad..970a464c 100644 --- a/matchbox_socket/src/webrtc_socket/native.rs +++ b/matchbox_socket/src/webrtc_socket/native.rs @@ -54,7 +54,7 @@ impl Signaller for NativeSignaller { Err(e) => { if let Some(attempts) = attempts.as_mut() { if *attempts <= 1 { - return Err(SignalingError::ConnectionFailed(Box::new(e))); + return Err(SignalingError::NegotiationFailed(Box::new(e))); } else { *attempts -= 1; warn!("connection to signaling server failed, {attempts} attempt(s) remain"); diff --git a/matchbox_socket/src/webrtc_socket/socket.rs b/matchbox_socket/src/webrtc_socket/socket.rs index 4beb662b..5037efcc 100644 --- a/matchbox_socket/src/webrtc_socket/socket.rs +++ b/matchbox_socket/src/webrtc_socket/socket.rs @@ -296,16 +296,31 @@ impl WebRtcSocketBuilder { peer_state_tx, messages_from_peers_tx, ) + // Transform the source into a user-error. .map(|f| { f.map_err(|e| match e { - SignalingError::Undeliverable(_) => Error::Disconnected, - SignalingError::StreamExhausted => Error::Disconnected, - SignalingError::UnknownFormat => Error::Communication(e), - SignalingError::ConnectionFailed(_) => Error::ConnectionFailed, - SignalingError::Socket(_) => Error::Disconnected, - SignalingError::Packet(_) => Error::Disconnected, + SignalingError::Undeliverable(source) => Error::Disconnected { + source: source.into(), + }, + SignalingError::StreamExhausted => Error::Disconnected { + source: SignalingError::StreamExhausted, + }, + SignalingError::UnknownFormat => Error::Runtime { + source: SignalingError::UnknownFormat, + }, + SignalingError::NegotiationFailed(source) => { + Error::ConnectionFailed { source: *source } + } + SignalingError::Socket(source) => Error::Disconnected { + source: source.into(), + }, + SignalingError::Packet(source) => Error::Disconnected { + source: source.into(), + }, #[cfg(target_arch = "wasm32")] - SignalingError::JsPacket(_) => Error::Disconnected, + SignalingError::JsPacket(source) => Error::Disconnected { + source: source.into(), + }, }) }); @@ -714,7 +729,10 @@ mod test { let result = fut.await; assert!(result.is_err()); - assert!(matches!(result.unwrap_err(), Error::ConnectionFailed)); + assert!(matches!( + result.unwrap_err(), + Error::ConnectionFailed { .. } + )); } #[futures_test::test] @@ -726,6 +744,9 @@ mod test { let result = loop_fut.await; assert!(result.is_err()); - assert!(matches!(result.unwrap_err(), Error::ConnectionFailed,)); + assert!(matches!( + result.unwrap_err(), + Error::ConnectionFailed { .. }, + )); } } diff --git a/matchbox_socket/src/webrtc_socket/wasm.rs b/matchbox_socket/src/webrtc_socket/wasm.rs index 3946ec6f..f1e19199 100644 --- a/matchbox_socket/src/webrtc_socket/wasm.rs +++ b/matchbox_socket/src/webrtc_socket/wasm.rs @@ -39,7 +39,7 @@ impl Signaller for WasmSignaller { Err(e) => { if let Some(attempts) = attempts.as_mut() { if *attempts <= 1 { - return Err(SignalingError::ConnectionFailed(Box::new(e))); + return Err(SignalingError::NegotiationFailed(Box::new(e))); } else { *attempts -= 1; warn!("connection to signaling server failed, {attempts} attempt(s) remain");