From cd0815844930e920d01b8ea655d1ff02dea423bd Mon Sep 17 00:00:00 2001 From: Sonia Zorba Date: Sun, 15 Sep 2024 00:09:10 +0200 Subject: [PATCH] Handle non-200 status codes with JSON payload in GitHub API responses --- src/main.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c4cbbf..807a167 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,7 +263,11 @@ impl eframe::App for Flasher { if internet { let json = String::from_utf8(data).unwrap(); - std::fs::write(path, json).unwrap(); + if handle.response_code().unwrap() == 200 { + std::fs::write(path, json).unwrap(); + } else { + std::fs::write(path, format!("GitHub Error: {}", json)).unwrap(); + } } else { std::fs::write(path, "No Internet").unwrap(); } @@ -290,12 +294,18 @@ impl eframe::App for Flasher { if json.is_err() { self.toasts.dismiss_all_toasts(); self.toasts - .error("Could not access github, Online Files Will be Unavailable") + .error("Could not access GitHub, Online Files Will be Unavailable") .set_duration(Some(Duration::from_secs(5))) .set_closable(false); - self.config - .logs - .push_str("PineFlash: Invalid json downloaded, could not fetch versions.\n"); + if string.starts_with("GitHub Error: ") { + self.config + .logs + .push_str(format!("PineFlash: {}. Could not fetch versions.\n", string.as_str()).as_str()); + } else { + self.config + .logs + .push_str("PineFlash: Invalid JSON downloaded, could not fetch versions.\n"); + } self.config.versions_checked = true; } else { for i in 0..3 {