Skip to content

Commit ba3d7bd

Browse files
authored
Avoid panics returning error instead (#509)
# Description Improve robustness by replacing `.expect()` calls with proper error handling in `opening.rs`. Instead of panicking when `on_noise_channel_open()` or `on_noise_channel_data()` fail, the errors are now logged and the connection is gracefully closed by returning `WebRtcEvent::ConnectionClosed` Closes #350 Closes ChainSafe/gossamer-parity#77
1 parent 6d1e880 commit ba3d7bd

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/transport/webrtc/opening.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,15 @@ impl OpeningWebRtcConnection {
409409
continue;
410410
}
411411

412-
// TODO: https://github.com/paritytech/litep2p/issues/350 no expect
413-
self.on_noise_channel_open().expect("to succeed");
412+
if let Err(error) = self.on_noise_channel_open() {
413+
tracing::debug!(
414+
target: LOG_TARGET,
415+
connection_id = ?self.connection_id,
416+
?error,
417+
"noise channel open failed",
418+
);
419+
return WebRtcEvent::ConnectionClosed;
420+
}
414421
}
415422
Event::ChannelData(data) => {
416423
tracing::trace!(
@@ -428,8 +435,18 @@ impl OpeningWebRtcConnection {
428435
continue;
429436
}
430437

431-
// TODO: https://github.com/paritytech/litep2p/issues/350 no expect
432-
return self.on_noise_channel_data(data.data).expect("to succeed");
438+
match self.on_noise_channel_data(data.data) {
439+
Ok(event) => return event,
440+
Err(error) => {
441+
tracing::debug!(
442+
target: LOG_TARGET,
443+
connection_id = ?self.connection_id,
444+
?error,
445+
"noise channel data handling failed",
446+
);
447+
return WebRtcEvent::ConnectionClosed;
448+
}
449+
}
433450
}
434451
Event::ChannelClose(channel_id) => {
435452
tracing::debug!(target: LOG_TARGET, ?channel_id, "channel closed");

0 commit comments

Comments
 (0)