From 10c099e03bd19994692e3977e302a97a7721bbcf Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 18 Oct 2022 09:04:25 +0200 Subject: [PATCH] Handle connection errors in a better way --- rust/sea_battle_cli_player/src/main.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/rust/sea_battle_cli_player/src/main.rs b/rust/sea_battle_cli_player/src/main.rs index b3ef9b2..cc991cc 100644 --- a/rust/sea_battle_cli_player/src/main.rs +++ b/rust/sea_battle_cli_player/src/main.rs @@ -149,17 +149,17 @@ async fn run_app(terminal: &mut Terminal) -> Res { let client = match choice { ScreenResult::Ok(SelectPlayModeResult::PlayRandom) => { - Client::start_random_play(&username).await? + Client::start_random_play(&username).await } // Play against bot ScreenResult::Ok(SelectPlayModeResult::PlayAgainstBot) => { - Client::start_bot_play(&rules).await? + Client::start_bot_play(&rules).await } // Create invite ScreenResult::Ok(SelectPlayModeResult::CreateInvite) => { - Client::start_create_invite(&rules, &username).await? + Client::start_create_invite(&rules, &username).await } // Join invite @@ -175,14 +175,22 @@ async fn run_app(terminal: &mut Terminal) -> Res { }; PopupScreen::new("🔌 Connecting...").show_once(terminal)?; - Client::start_accept_invite(code, &username).await? + Client::start_accept_invite(code, &username).await } ScreenResult::Canceled | ScreenResult::Ok(SelectPlayModeResult::Exit) => return Ok(()), }; - // Display game screen - GameScreen::new(client).show(terminal).await?; + match client { + Ok(client) => { + // Display game screen + GameScreen::new(client).show(terminal).await?; + } + Err(e) => { + log::error!("Failed to connect to server: {}", e); + PopupScreen::new("❌ Failed to connect to server!").show(terminal)?; + } + }; } }