handle some networking errors

assume we are up to date if we can't fetch latest version
This commit is contained in:
2024-09-06 11:10:05 +02:00
parent d267d53c49
commit 3963f8fc9b
4 changed files with 30 additions and 13 deletions

View File

@@ -9,12 +9,10 @@
![GitHub tag (with filter)](https://img.shields.io/github/v/tag/mxve/alterware-launcher?filter=!v*-pre&style=flat-square&label=Latest%20release&labelColor=F3F8FF&color=E26EE5) ![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/mxve/alterware-launcher?style=flat-square&label=Release%20date&labelColor=F3F8FF&color=E26EE5) ![GitHub all releases](https://img.shields.io/github/downloads/mxve/alterware-launcher/total?style=flat-square&label=Total%20downloads&labelColor=F3F8FF&color=E26EE5) ![GitHub tag (with filter)](https://img.shields.io/github/v/tag/mxve/alterware-launcher?filter=!v*-pre&style=flat-square&label=Latest%20release&labelColor=F3F8FF&color=E26EE5) ![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/mxve/alterware-launcher?style=flat-square&label=Release%20date&labelColor=F3F8FF&color=E26EE5) ![GitHub all releases](https://img.shields.io/github/downloads/mxve/alterware-launcher/total?style=flat-square&label=Total%20downloads&labelColor=F3F8FF&color=E26EE5)
</div> </div>
---
> [!IMPORTANT] > [!IMPORTANT]
> **Only legitimate copies of the games are supported. If you don't own the game, please buy it.** > **Only legitimate copies of the games are supported. If you don't own the game, please buy it.**
## Table of Contents ## 🗺️ Table of Contents
<a href="https://alterware.dev"><img src=".github/images/logo.png" align="right" width="128" height="128"></a> <a href="https://alterware.dev"><img src=".github/images/logo.png" align="right" width="128" height="128"></a>

View File

@@ -1,6 +1,6 @@
use semver::Version; use semver::Version;
pub async fn latest_tag(owner: &str, repo: &str) -> String { pub async fn latest_tag(owner: &str, repo: &str) -> Result<String, Box<dyn std::error::Error>> {
let github_body = crate::http_async::get_body_string( let github_body = crate::http_async::get_body_string(
format!( format!(
"https://api.github.com/repos/{}/{}/releases/latest", "https://api.github.com/repos/{}/{}/releases/latest",
@@ -8,23 +8,32 @@ pub async fn latest_tag(owner: &str, repo: &str) -> String {
) )
.as_str(), .as_str(),
) )
.await .await?;
.unwrap();
let github_json: serde_json::Value = serde_json::from_str(&github_body).unwrap(); let github_json: serde_json::Value = serde_json::from_str(&github_body)?;
if let Some(tag_name) = github_json.get("tag_name") { if let Some(tag_name) = github_json.get("tag_name") {
if let Some(tag_name_str) = tag_name.as_str() { if let Some(tag_name_str) = tag_name.as_str() {
return tag_name_str.to_string().replace('"', ""); return Ok(tag_name_str.to_string().replace('"', ""));
} }
} }
"0.0.0".to_string() Ok("0.0.0".to_string())
} }
pub async fn latest_version(owner: &str, repo: &str) -> Version { pub async fn latest_version(owner: &str, repo: &str) -> Version {
let tag = latest_tag(owner, repo).await.replace('v', ""); match latest_tag(owner, repo).await {
Version::parse(&tag).unwrap() Ok(tag) => {
let cleaned_tag = tag.replace('v', "");
Version::parse(&cleaned_tag).unwrap_or_else(|_| Version::new(0, 0, 0))
}
Err(_) => {
crate::println_error!(
"Failed to get latest version for {owner}/{repo}, assuming we are up to date."
);
Version::new(0, 0, 0)
}
}
} }
pub fn latest_release_url(owner: &str, repo: &str) -> String { pub fn latest_release_url(owner: &str, repo: &str) -> String {

View File

@@ -9,7 +9,13 @@ use std::fs;
use std::path::Path; use std::path::Path;
pub async fn remote_revision() -> u16 { pub async fn remote_revision() -> u16 {
misc::rev_to_int(&github::latest_tag(GH_IW4X_OWNER, GH_IW4X_REPO).await) match github::latest_tag(GH_IW4X_OWNER, GH_IW4X_OWNER).await {
Ok(tag) => misc::rev_to_int(&tag),
Err(_) => {
crate::println_error!("Failed to get latest version for {GH_IW4X_OWNER}/{GH_IW4X_OWNER}, assuming we are up to date.");
0
}
}
} }
pub async fn update(dir: &Path, cache: &mut structs::Cache) { pub async fn update(dir: &Path, cache: &mut structs::Cache) {

View File

@@ -680,7 +680,11 @@ async fn main() {
let games_json = let games_json =
http_async::get_body_string(format!("{}/games.json", MASTER.lock().unwrap()).as_str()) http_async::get_body_string(format!("{}/games.json", MASTER.lock().unwrap()).as_str())
.await .await
.unwrap(); .unwrap_or_else(|error| {
crate::println_error!("Failed to get games.json: {:#?}", error);
misc::stdin();
std::process::exit(1);
});
let games: Vec<Game> = serde_json::from_str(&games_json).unwrap_or_else(|error| { let games: Vec<Game> = serde_json::from_str(&games_json).unwrap_or_else(|error| {
crate::println_error!("Error parsing games.json: {:#?}", error); crate::println_error!("Error parsing games.json: {:#?}", error);
misc::stdin(); misc::stdin();