Skip to content

Commit ec99641

Browse files
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>
1 parent 1e044c5 commit ec99641

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- smoother large file download&proxy support (#463)
12+
- Add GitHub API errors to clarify what failed (#464)
1213

1314
### Fixed
1415
- When queriying GitHub for the list of releases, retrieve more items (#462)

src/error.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ pub enum Error {
77
CreateDirectory(String),
88

99
#[diagnostic(code(espup::toolchain::rust::query_github))]
10-
#[error("Failed to query GitHub API")]
11-
GithubQuery,
10+
#[error("Failed to query GitHub API: Rate Limiting")]
11+
GithubRateLimit,
12+
13+
#[diagnostic(code(espup::toolchain::rust::query_github))]
14+
#[error("Failed to query GitHub API: Invalid Github token")]
15+
GithubTokenInvalid,
1216

1317
#[diagnostic(code(espup::toolchain::rust::install_riscv_target))]
1418
#[error("Failed to Install RISC-V targets for '{0}' toolchain")]

src/toolchain/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()>
233233
toolchain_version.clone()
234234
}
235235
} else {
236-
XtensaRust::get_latest_version().await?
236+
// Get the latest version of the Xtensa Rust toolchain. If that fails, return an error::GithubTokenInvalid
237+
XtensaRust::get_latest_version()
238+
.await
239+
.map_err(|_| Error::GithubTokenInvalid)?
237240
};
238241
let toolchain_dir = get_rustup_home().join("toolchains").join(args.name);
239242
let llvm: Llvm = Llvm::new(
@@ -364,6 +367,7 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
364367
header::ACCEPT,
365368
"application/vnd.github+json".parse().unwrap(),
366369
);
370+
367371
headers.insert("X-GitHub-Api-Version", "2022-11-28".parse().unwrap());
368372
if let Some(token) = env::var_os("GITHUB_TOKEN") {
369373
debug!("Auth header added");
@@ -375,23 +379,27 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
375379
);
376380
}
377381
let client = build_proxy_blocking_client()?;
378-
let json = retry(
382+
let json: Result<serde_json::Value, Error> = retry(
379383
Fixed::from_millis(100).take(5),
380384
|| -> Result<serde_json::Value, Error> {
381385
let res = client.get(url).headers(headers.clone()).send()?.text()?;
382386
if res.contains(
383387
"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting",
384388
) {
385-
warn!("GitHub rate limit exceeded");
386-
return Err(Error::GithubQuery);
389+
return Err(Error::GithubRateLimit);
387390
}
391+
392+
if res.contains("Bad credentials") {
393+
return Err(Error::GithubTokenInvalid);
394+
}
395+
388396
let json: serde_json::Value =
389397
serde_json::from_str(&res).map_err(|_| Error::SerializeJson)?;
390398
Ok(json)
391399
},
392400
)
393-
.unwrap();
394-
Ok(json)
401+
.map_err(|err| err.error);
402+
json
395403
}
396404

397405
/// Checks if the directory exists and deletes it if it does.

src/toolchain/rust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use tokio::fs::{remove_dir_all, remove_file};
3333
/// Xtensa Rust Toolchain repository
3434
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
3535
"https://github.com/esp-rs/rust-build/releases/download";
36+
3637
/// Xtensa Rust Toolchain API URL
3738
const XTENSA_RUST_LATEST_API_URL: &str =
3839
"https://api.github.com/repos/esp-rs/rust-build/releases/latest";

0 commit comments

Comments
 (0)