Add some errors for GitHub API (#464)

* add preliminary check for invalid github token

* mapping errors

* adjust log information and format

* clippy fixes

* feat: Avoid having a fallback version

* docs: Add changelog

---------

Co-authored-by: leongross <leon.gross@rub.de>
This commit is contained in:
Sergio Gasquez Arcos 2024-12-13 10:22:13 +01:00 committed by GitHub
parent 1e044c5eb4
commit ec9964196c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 8 deletions

View File

@ -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)

View File

@ -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")]

View File

@ -233,7 +233,10 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()>
toolchain_version.clone()
}
} else {
XtensaRust::get_latest_version().await?
// 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(
@ -364,6 +367,7 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
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");
@ -375,23 +379,27 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
);
}
let client = build_proxy_blocking_client()?;
let json = retry(
let json: Result<serde_json::Value, Error> = retry(
Fixed::from_millis(100).take(5),
|| -> Result<serde_json::Value, Error> {
let res = client.get(url).headers(headers.clone()).send()?.text()?;
if res.contains(
"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") {
return Err(Error::GithubTokenInvalid);
}
let json: serde_json::Value =
serde_json::from_str(&res).map_err(|_| Error::SerializeJson)?;
Ok(json)
},
)
.unwrap();
Ok(json)
.map_err(|err| err.error);
json
}
/// Checks if the directory exists and deletes it if it does.

View File

@ -33,6 +33,7 @@ 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 API URL
const XTENSA_RUST_LATEST_API_URL: &str =
"https://api.github.com/repos/esp-rs/rust-build/releases/latest";