From b47a032e3c3b745eed8bf21f2d107e7b6d6677c2 Mon Sep 17 00:00:00 2001 From: Zefanja Jobse Date: Fri, 22 Sep 2023 20:39:11 +0200 Subject: [PATCH] add error handler for api --- src/main.rs | 44 ++++++++++++++++++++++++++++---------------- src/structs.rs | 7 ++++++- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index c56aa80..fc3e0df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ fn main() { message_timeout_mins: 8, game: structs::Games::from("bf1"), launcher: structs::Launchers::from("ea_desktop"), - endpoint: "https://manager-api.gametools.network".into() + endpoint: "https://manager-api.gametools.network".into(), }; cfg.game_location = actions::game::find_game(&cfg); cfg.link2ea_location = actions::launchers::find_link2ea(); @@ -122,21 +122,33 @@ fn main() { .timeout(Duration::new(10, 0)) .call() { - Ok(response) => match response.into_json::() { - Ok(seeder_info) => { - functions::seed_server::start( - seeder_info, - &mut old_seeder_info, - &cfg, - &game_running, - &retry_launch, - &message_running_clone, - ); - } - Err(e) => { - log::error!("Failed to get info about server to join: {}", e); - log::info!("reconnecting..."); - } + Ok(response) => match response.header("type") { + Some(_) => match response.into_json::() { + Ok(error_return) => { + log::error!("Error: {}", error_return.error); + log::info!("retrying..."); + } + Err(e) => { + log::error!("Incorrect error return: {}", e); + log::info!("retrying..."); + } + }, + None => match response.into_json::() { + Ok(seeder_info) => { + functions::seed_server::start( + seeder_info, + &mut old_seeder_info, + &cfg, + &game_running, + &retry_launch, + &message_running_clone, + ); + } + Err(e) => { + log::error!("Failed to get info about server to join: {}", e); + log::info!("reconnecting..."); + } + }, }, Err(e) => { log::error!("Failed to connect to backend: {}", e); diff --git a/src/structs.rs b/src/structs.rs index 81022e5..a963f61 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -46,6 +46,11 @@ pub struct CurrentServer { pub rejoin: bool, } +#[derive(Deserialize, PartialEq, Eq, Clone, Debug)] +pub struct Error { + pub error: String, +} + #[derive(Debug)] pub struct GameInfo { pub is_running: bool, @@ -71,7 +76,7 @@ impl ::std::default::Default for SeederConfig { message_timeout_mins: 8, game: Games::from("bf1"), launcher: Launchers::from("ea_desktop"), - endpoint: "https://manager-api.gametools.network".into() + endpoint: "https://manager-api.gametools.network".into(), }; cfg.game_location = actions::game::find_game(&cfg); cfg.link2ea_location = actions::launchers::find_link2ea();