Skip to content

Commit

Permalink
build: attach source to error
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Sep 18, 2023
1 parent 6fb0a89 commit cce25e2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
17 changes: 13 additions & 4 deletions matchbox_socket/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
6 changes: 3 additions & 3 deletions matchbox_socket/src/webrtc_socket/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum SignalingError {
UnknownFormat,

#[error("failed to establish initial connection: {0}")]
ConnectionFailed(#[from] Box<SignalingError>),
NegotiationFailed(#[from] Box<SignalingError>),

// Native
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion matchbox_socket/src/webrtc_socket/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
39 changes: 30 additions & 9 deletions matchbox_socket/src/webrtc_socket/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,31 @@ impl<C: BuildablePlurality> WebRtcSocketBuilder<C> {
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(),
},
})
});

Expand Down Expand Up @@ -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]
Expand All @@ -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 { .. },
));
}
}
2 changes: 1 addition & 1 deletion matchbox_socket/src/webrtc_socket/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit cce25e2

Please sign in to comment.