From 8097bfa40859374789cb210fb18ff53c7f738191 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 3 Mar 2022 10:04:55 +0100 Subject: [PATCH] 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. --- lychee-bin/src/main.rs | 12 ++++++++++++ lychee-bin/tests/cli.rs | 8 ++++++-- lychee-lib/src/client.rs | 9 +++++++-- lychee-lib/src/types/error.rs | 4 ++++ lychee-lib/src/types/status.rs | 6 ------ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lychee-bin/src/main.rs b/lychee-bin/src/main.rs index 9364d99..18a17bb 100644 --- a/lychee-bin/src/main.rs +++ b/lychee-bin/src/main.rs @@ -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(()) } diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index 68caf26..5c7493a 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -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] diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index 66d6cb4..eb75dd4 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -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 diff --git a/lychee-lib/src/types/error.rs b/lychee-lib/src/types/error.rs index 26ac549..a91dc6b 100644 --- a/lychee-lib/src/types/error.rs +++ b/lychee-lib/src/types/error.rs @@ -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), diff --git a/lychee-lib/src/types/status.rs b/lychee-lib/src/types/status.rs index f22ecec..624755c 100644 --- a/lychee-lib/src/types/status.rs +++ b/lychee-lib/src/types/status.rs @@ -190,12 +190,6 @@ impl From for Status { } } -impl From for Status { - fn from(e: octocrab::Error) -> Self { - Self::Error(ErrorKind::GithubRequest(e)) - } -} - impl From for Status { fn from(s: CacheStatus) -> Self { Self::Cached(s)