Skip to content

Commit 5a4aad7

Browse files
Mahdiglmo2sh
andauthored
Improve error handling (#1560)
Co-authored-by: o2sh <[email protected]>
1 parent 82a5009 commit 5a4aad7

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

manifest/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub fn get_manifests<P: AsRef<Path>>(path: P) -> Result<Vec<Manifest>> {
4242
}
4343

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

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

5960
fn parse_npm_manifest(path: &Path) -> Result<Manifest> {
60-
let package = npm_package_json::Package::from_path(path)?;
61+
let package = npm_package_json::Package::from_path(path)
62+
.with_context(|| format!("Failed to parse package.json at '{}'", path.display()))?;
6163
Ok(Manifest {
6264
manifest_type: ManifestType::Npm,
6365
number_of_dependencies: package.dependencies.len(),

src/info/langs/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ pub fn get_loc_by_language_sorted(
2121
) -> Result<Vec<(Language, usize)>> {
2222
let locs = get_locs(dir, globs_to_exclude, language_types, include_hidden);
2323

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

2733
let loc_by_language_sorted = sort_by_loc(loc_by_language);
2834

src/info/license.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl Detector {
2222
pub fn new() -> Result<Self> {
2323
match Store::from_cache(CACHE_DATA) {
2424
Ok(store) => Ok(Self { store }),
25-
Err(_) => {
26-
bail!("Could not initialize the license detector")
25+
Err(e) => {
26+
bail!("Could not initialize the license detector: {}", e)
2727
}
2828
}
2929
}

src/info/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,16 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
147147
&repo,
148148
cli_options.info.hide_token,
149149
cli_options.info.http_url,
150-
)?;
150+
)
151+
.context("Failed to determine repository URL")?;
151152

152153
let git_metrics = traverse_commit_graph(
153154
&repo,
154155
cli_options.info.no_bots.clone(),
155156
cli_options.info.churn_pool_size,
156157
cli_options.info.no_merges,
157-
)?;
158+
)
159+
.context("Failed to traverse Git commit history")?;
158160
let true_color = match cli_options.ascii.true_color {
159161
When::Always => true,
160162
When::Never => false,

src/ui/printer.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ pub struct Printer<W> {
3131

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

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

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

0 commit comments

Comments
 (0)