Print Github token error once at the end (#537)

Print original reqwest error for every Github link.
It contains more information about the underlying error.

Only print a message about the Github token at the
end if it's not set and there were Github errors.
This commit is contained in:
Matthias 2022-03-03 10:04:55 +01:00 committed by GitHub
parent 4c51fce22f
commit 8097bfa408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 10 deletions

View file

@ -58,6 +58,7 @@
#![deny(anonymous_parameters, macro_use_extern_crate, pointer_structural_match)]
#![deny(missing_docs)]
use color::YELLOW;
use lychee_lib::Collector;
// required for apple silicon
use ring as _;
@ -82,6 +83,7 @@ mod writer;
use crate::{
cache::{Cache, StoreExt},
color::color,
options::{Config, Format, LycheeOptions, LYCHEE_CACHE_FILE, LYCHEE_IGNORE_FILE},
stats::ResponseStats,
writer::StatsWriter,
@ -256,6 +258,11 @@ fn write_stats(stats: ResponseStats, cfg: &Config) -> Result<()> {
};
let is_empty = stats.is_empty();
let github_issues = stats
.fail_map
.values()
.flatten()
.any(|body| body.uri.domain() == Some("github.com"));
let formatted = writer.write(stats)?;
if let Some(output) = &cfg.output {
@ -268,5 +275,10 @@ fn write_stats(stats: ResponseStats, cfg: &Config) -> Result<()> {
// we assume that the formatted stats don't have a final newline
writeln!(io::stdout(), "{formatted}")?;
}
if github_issues && cfg.github_token.is_none() {
let mut f = io::stdout();
color!(f, YELLOW, "\u{1f4a1} There were issues with Github URLs. You could try setting a Github token and running lychee again.",)?;
}
Ok(())
}

View file

@ -276,8 +276,12 @@ mod cli {
.assert()
.failure()
.code(2)
.stdout(contains("https://github.com/mre/idiomatic-rust-doesnt-exist-man | \
GitHub token not specified. To check GitHub links reliably, use `--github-token` flag / `GITHUB_TOKEN` env var."));
.stdout(contains(
"https://github.com/mre/idiomatic-rust-doesnt-exist-man | Network error: Not Found",
))
.stdout(contains(
"There were issues with Github URLs. You could try setting a Github token and running lychee again.",
));
}
#[tokio::test]

View file

@ -270,7 +270,7 @@ impl ClientBuilder {
Octocrab::builder()
.personal_token(token.clone())
.build()
.map_err(ErrorKind::GithubRequest)?,
.map_err(ErrorKind::BuildGithubClient)?,
),
_ => None,
};
@ -417,7 +417,12 @@ impl Client {
// Pull out the heavy machinery in case of a failed normal request.
// This could be a GitHub URL and we ran into the rate limiter.
if let Some(github_uri) = uri.gh_org_and_repo() {
return self.check_github(github_uri).await;
let status = self.check_github(github_uri).await;
// Only return Github status in case of success
// Otherwise return the original error, which has more information
if status.is_success() {
return status;
}
}
status

View file

@ -34,6 +34,9 @@ pub enum ErrorKind {
/// The network client required for making requests cannot be created
#[error("Error creating request client")]
BuildRequestClient(#[source] reqwest::Error),
/// The Github client required for making requests cannot be created
#[error("Error creating Github client")]
BuildGithubClient(#[source] octocrab::Error),
/// Network error while using Github API
#[error("Network error (GitHub client)")]
GithubRequest(#[from] octocrab::Error),
@ -136,6 +139,7 @@ impl Hash for ErrorKind {
Self::NetworkRequest(e) => e.to_string().hash(state),
Self::ReadResponseBody(e) => e.to_string().hash(state),
Self::BuildRequestClient(e) => e.to_string().hash(state),
Self::BuildGithubClient(e) => e.to_string().hash(state),
Self::GithubRequest(e) => e.type_id().hash(state),
Self::InvalidGithubUrl(s) => s.hash(state),
Self::DirTraversal(e) => e.to_string().hash(state),

View file

@ -190,12 +190,6 @@ impl From<reqwest::Error> for Status {
}
}
impl From<octocrab::Error> for Status {
fn from(e: octocrab::Error) -> Self {
Self::Error(ErrorKind::GithubRequest(e))
}
}
impl From<CacheStatus> for Status {
fn from(s: CacheStatus) -> Self {
Self::Cached(s)