From 4ba6388cc35f66fa5cbb53ec0c078e271e72721d Mon Sep 17 00:00:00 2001 From: leongross Date: Sun, 12 Nov 2023 15:25:19 +0100 Subject: [PATCH 1/6] add preliminary check for invalid github token --- src/toolchain/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index 69652007..ed02e831 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -367,6 +367,12 @@ pub fn github_query(url: &str) -> Result { headers.insert("X-GitHub-Api-Version", "2022-11-28".parse().unwrap()); if let Some(token) = env::var_os("GITHUB_TOKEN") { debug!("Auth header added"); + match token.to_str() { + Some(str) => { + dbg!(str); + } + _ => {} + } headers.insert( "Authorization", format!("Bearer {}", token.to_string_lossy()) @@ -385,12 +391,19 @@ pub fn github_query(url: &str) -> Result { warn!("GitHub rate limit exceeded"); return Err(Error::GithubQuery); } + + if res.contains("Bad credentials") { + warn!("Github token credentials invalid"); + return Err(Error::GithubQuery); + } + let json: serde_json::Value = serde_json::from_str(&res).map_err(|_| Error::SerializeJson)?; Ok(json) }, ) .unwrap(); + Ok(json) } From a2fd367b86eb2fba3a6c224011a021073108145c Mon Sep 17 00:00:00 2001 From: leongross Date: Mon, 13 Nov 2023 14:22:09 +0100 Subject: [PATCH 2/6] mapping errors --- src/error.rs | 8 ++++++-- src/toolchain/mod.rs | 22 +++++++++++++++------- src/toolchain/rust.rs | 4 ++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/error.rs b/src/error.rs index d3452070..e3920b9d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,8 +7,12 @@ pub enum Error { CreateDirectory(String), #[diagnostic(code(espup::toolchain::rust::query_github))] - #[error("Failed to query GitHub API")] - GithubQuery, + #[error("Failed to query GitHub API: Rate Limiting")] + GithubRateLimit, + + #[diagnostic(code(espup::toolchain::rust::query_github))] + #[error("Failed to query GitHub API: Invalid Github token")] + GithubTokenInvalid, #[diagnostic(code(espup::toolchain::rust::install_riscv_target))] #[error("Failed to Install RISC-V targets for '{0}' toolchain")] diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index ed02e831..cfc3b05b 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -15,6 +15,7 @@ use crate::{ }, }; use async_trait::async_trait; +use clap::builder::OsStr; use flate2::bufread::GzDecoder; use log::{debug, info, warn}; use miette::Result; @@ -233,7 +234,13 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> toolchain_version.clone() } } else { - XtensaRust::get_latest_version().await? + // if there was an error getting the newest version from github, use a hardcoded fallback + // XtensaRust::get_latest_version().await? + XtensaRust::get_latest_version().await.unwrap_or_else(|e| { + print!("Error getting newest toolchain version {e}. Using fallback version."); + return String::from("v1.0.0"); + }) + // .unwrap_or_else(|e| return Ok(())); }; let toolchain_dir = get_rustup_home().join("toolchains").join(args.name); let llvm: Llvm = Llvm::new( @@ -364,6 +371,7 @@ pub fn github_query(url: &str) -> Result { header::ACCEPT, "application/vnd.github+json".parse().unwrap(), ); + headers.insert("X-GitHub-Api-Version", "2022-11-28".parse().unwrap()); if let Some(token) = env::var_os("GITHUB_TOKEN") { debug!("Auth header added"); @@ -381,7 +389,7 @@ pub fn github_query(url: &str) -> Result { ); } let client = build_proxy_blocking_client()?; - let json = retry( + let json: Result = retry( Fixed::from_millis(100).take(5), || -> Result { let res = client.get(url).headers(headers.clone()).send()?.text()?; @@ -389,12 +397,12 @@ pub fn github_query(url: &str) -> Result { "https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting", ) { warn!("GitHub rate limit exceeded"); - return Err(Error::GithubQuery); + return Err(Error::GithubRateLimit); } if res.contains("Bad credentials") { warn!("Github token credentials invalid"); - return Err(Error::GithubQuery); + return Err(Error::GithubTokenInvalid); } let json: serde_json::Value = @@ -402,9 +410,9 @@ pub fn github_query(url: &str) -> Result { Ok(json) }, ) - .unwrap(); - - Ok(json) + .map_err(|err| err.error); + // dbg!(&json); + json } /// Checks if the directory exists and deletes it if it does. diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index fb5b7678..6b453d34 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -33,6 +33,10 @@ use tokio::fs::{remove_dir_all, remove_file}; /// Xtensa Rust Toolchain repository const DEFAULT_XTENSA_RUST_REPOSITORY: &str = "https://github.com/esp-rs/rust-build/releases/download"; + +/// Xtensa Rust Toolchain fallback version +static TOOLCHAIN_FALLBACK: &str = "v1.57.0.2"; + /// Xtensa Rust Toolchain API URL const XTENSA_RUST_LATEST_API_URL: &str = "https://api.github.com/repos/esp-rs/rust-build/releases/latest"; From 20029f313edf4a0d5222626442c405c7572e11cf Mon Sep 17 00:00:00 2001 From: leongross Date: Mon, 13 Nov 2023 14:33:20 +0100 Subject: [PATCH 3/6] adjust log information and format --- src/toolchain/mod.rs | 17 ++++++----------- src/toolchain/rust.rs | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index cfc3b05b..c15a9fd6 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -235,12 +235,14 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> } } else { // if there was an error getting the newest version from github, use a hardcoded fallback - // XtensaRust::get_latest_version().await? XtensaRust::get_latest_version().await.unwrap_or_else(|e| { - print!("Error getting newest toolchain version {e}. Using fallback version."); - return String::from("v1.0.0"); + warn!( + "Error getting newest toolchain version {}. Using fallback version '{}'", + e, + rust::TOOLCHAIN_FALLBACK + ); + return String::from(rust::TOOLCHAIN_FALLBACK); }) - // .unwrap_or_else(|e| return Ok(())); }; let toolchain_dir = get_rustup_home().join("toolchains").join(args.name); let llvm: Llvm = Llvm::new( @@ -375,12 +377,6 @@ pub fn github_query(url: &str) -> Result { headers.insert("X-GitHub-Api-Version", "2022-11-28".parse().unwrap()); if let Some(token) = env::var_os("GITHUB_TOKEN") { debug!("Auth header added"); - match token.to_str() { - Some(str) => { - dbg!(str); - } - _ => {} - } headers.insert( "Authorization", format!("Bearer {}", token.to_string_lossy()) @@ -411,7 +407,6 @@ pub fn github_query(url: &str) -> Result { }, ) .map_err(|err| err.error); - // dbg!(&json); json } diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index 6b453d34..22c9c41d 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -35,7 +35,7 @@ const DEFAULT_XTENSA_RUST_REPOSITORY: &str = "https://github.com/esp-rs/rust-build/releases/download"; /// Xtensa Rust Toolchain fallback version -static TOOLCHAIN_FALLBACK: &str = "v1.57.0.2"; +pub const TOOLCHAIN_FALLBACK: &str = "1.57.0.2"; /// Xtensa Rust Toolchain API URL const XTENSA_RUST_LATEST_API_URL: &str = From eb584930a94506f696782c2376063465af38b7f2 Mon Sep 17 00:00:00 2001 From: leongross Date: Mon, 13 Nov 2023 15:24:41 +0100 Subject: [PATCH 4/6] clippy fixes --- src/toolchain/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index c15a9fd6..aa8a3fa3 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -15,7 +15,6 @@ use crate::{ }, }; use async_trait::async_trait; -use clap::builder::OsStr; use flate2::bufread::GzDecoder; use log::{debug, info, warn}; use miette::Result; @@ -241,7 +240,7 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> e, rust::TOOLCHAIN_FALLBACK ); - return String::from(rust::TOOLCHAIN_FALLBACK); + String::from(rust::TOOLCHAIN_FALLBACK) }) }; let toolchain_dir = get_rustup_home().join("toolchains").join(args.name); From d4c91bb1be765454552ab122dc833b67445520b1 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Thu, 12 Dec 2024 16:00:04 +0100 Subject: [PATCH 5/6] feat: Avoid having a fallback version --- src/toolchain/mod.rs | 15 ++++----------- src/toolchain/rust.rs | 3 --- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index aa8a3fa3..01f030e5 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -233,15 +233,10 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()> toolchain_version.clone() } } else { - // if there was an error getting the newest version from github, use a hardcoded fallback - XtensaRust::get_latest_version().await.unwrap_or_else(|e| { - warn!( - "Error getting newest toolchain version {}. Using fallback version '{}'", - e, - rust::TOOLCHAIN_FALLBACK - ); - String::from(rust::TOOLCHAIN_FALLBACK) - }) + // Get the latest version of the Xtensa Rust toolchain. If that fails, return an error::GithubTokenInvalid + XtensaRust::get_latest_version() + .await + .map_err(|_| Error::GithubTokenInvalid)? }; let toolchain_dir = get_rustup_home().join("toolchains").join(args.name); let llvm: Llvm = Llvm::new( @@ -391,12 +386,10 @@ pub fn github_query(url: &str) -> Result { if res.contains( "https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting", ) { - warn!("GitHub rate limit exceeded"); return Err(Error::GithubRateLimit); } if res.contains("Bad credentials") { - warn!("Github token credentials invalid"); return Err(Error::GithubTokenInvalid); } diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index 22c9c41d..05c83750 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -34,9 +34,6 @@ use tokio::fs::{remove_dir_all, remove_file}; const DEFAULT_XTENSA_RUST_REPOSITORY: &str = "https://github.com/esp-rs/rust-build/releases/download"; -/// Xtensa Rust Toolchain fallback version -pub const TOOLCHAIN_FALLBACK: &str = "1.57.0.2"; - /// Xtensa Rust Toolchain API URL const XTENSA_RUST_LATEST_API_URL: &str = "https://api.github.com/repos/esp-rs/rust-build/releases/latest"; From 3a84ea18d11828625d26f2d258e41be4e1b2bd0f Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 13 Dec 2024 09:38:42 +0100 Subject: [PATCH 6/6] docs: Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c134b59..d05d0666 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - smoother large file download&proxy support (#463) +- Add GitHub API errors to clarify what failed (#464) ### Fixed - When queriying GitHub for the list of releases, retrieve more items (#462)