Skip to content

Improve error handling #1560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions manifest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pub fn get_manifests<P: AsRef<Path>>(path: P) -> Result<Vec<Manifest>> {
}

fn parse_cargo_manifest(path: &Path) -> Result<Manifest> {
let m = cargo_toml::Manifest::from_path(path)?;
let m = cargo_toml::Manifest::from_path(path)
.with_context(|| format!("Failed to parse Cargo.toml at '{}'", path.display()))?;
let package = m.package.context("Not a package (only a workspace)")?;
let description = package.description().map(|v| v.into());

Expand All @@ -57,7 +58,8 @@ fn parse_cargo_manifest(path: &Path) -> Result<Manifest> {
}

fn parse_npm_manifest(path: &Path) -> Result<Manifest> {
let package = npm_package_json::Package::from_path(path)?;
let package = npm_package_json::Package::from_path(path)
.with_context(|| format!("Failed to parse package.json at '{}'", path.display()))?;
Ok(Manifest {
manifest_type: ManifestType::Npm,
number_of_dependencies: package.dependencies.len(),
Expand Down
2 changes: 1 addition & 1 deletion src/info/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn should_break(
return false;
}

max_churn_pool_size_opt.map_or(true, |max_churn_pool_size| {
max_churn_pool_size_opt.is_none_or(|max_churn_pool_size| {
number_of_diffs_computed >= max_churn_pool_size.min(total_number_of_commits)
})
}
Expand Down
10 changes: 8 additions & 2 deletions src/info/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ pub fn get_loc_by_language_sorted(
) -> Result<Vec<(Language, usize)>> {
let locs = get_locs(dir, globs_to_exclude, language_types, include_hidden);

let loc_by_language =
get_loc_by_language(&locs).context("Could not find any source code in this repository")?;
let loc_by_language = get_loc_by_language(&locs).with_context(|| {
format!(
"No source code found in the repository at '{}'. \
Note: Some language types (prose, data) are excluded by default. \
Consider using the '--type' option to include them.",
dir.display()
)
})?;

let loc_by_language_sorted = sort_by_loc(loc_by_language);

Expand Down
4 changes: 2 additions & 2 deletions src/info/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl Detector {
pub fn new() -> Result<Self> {
match Store::from_cache(CACHE_DATA) {
Ok(store) => Ok(Self { store }),
Err(_) => {
bail!("Could not initialize the license detector")
Err(e) => {
bail!("Could not initialize the license detector: {}", e)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,16 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
&repo,
cli_options.info.hide_token,
cli_options.info.http_url,
)?;
)
.context("Failed to determine repository URL")?;

let git_metrics = traverse_commit_graph(
&repo,
cli_options.info.no_bots.clone(),
cli_options.info.churn_pool_size,
cli_options.info.no_merges,
)?;
)
.context("Failed to traverse Git commit history")?;
let true_color = match cli_options.ascii.true_color {
When::Always => true,
When::Never => false,
Expand Down
15 changes: 9 additions & 6 deletions src/ui/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ pub struct Printer<W> {

impl<W: Write> Printer<W> {
pub fn new(writer: W, info: Info, cli_options: CliOptions) -> Result<Self> {
let image = match cli_options.image.image {
Some(p) => Some(image::open(p).context("Could not load the specified image")?),
None => None,
};
let image =
match cli_options.image.image {
Some(p) => Some(image::open(&p).with_context(|| {
format!("Could not load the image file at '{}'", p.display())
})?),
None => None,
};

let image_backend = if image.is_some() {
cli_options
Expand Down Expand Up @@ -83,7 +86,7 @@ impl<W: Write> Printer<W> {
let image_backend = self
.image_backend
.as_ref()
.context("Could not detect a supported image backend")?;
.context("No supported image backend detected on your system")?;

buf.push_str(
&image_backend
Expand All @@ -92,7 +95,7 @@ impl<W: Write> Printer<W> {
custom_image,
self.color_resolution,
)
.context("Error while drawing image")?,
.context("Failed to render the image in the terminal")?,
);
} else {
let mut logo_lines = if let Some(custom_ascii) = &self.ascii_input {
Expand Down