mirror of
https://github.com/esp-rs/espup.git
synced 2025-09-26 20:30:28 +00:00
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:
parent
1e044c5eb4
commit
ec9964196c
@ -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)
|
||||
|
@ -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")]
|
||||
|
@ -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.
|
||||
|
@ -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";
|
||||
|
Loading…
x
Reference in New Issue
Block a user