Skip to content

Commit e370760

Browse files
d-e-s-odanielocfb
authored andcommitted
Improve error context chaining
There are a bunch of occasions where we wrongly embed errors into a string and report the resulting string as a new error. Fix them by creating proper error chains. Signed-off-by: Daniel Müller <[email protected]>
1 parent d7d56d2 commit e370760

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

examples/tc_port_whitelist/src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#![allow(clippy::let_unit_value)]
2+
13
use std::os::unix::io::AsFd as _;
24

35
use anyhow::bail;
6+
use anyhow::Context as _;
47
use anyhow::Result;
58

69
use clap::Parser;
@@ -119,9 +122,11 @@ fn main() -> Result<()> {
119122
for (i, port) in opts.ports.iter().enumerate() {
120123
let key = (i as u32).to_ne_bytes();
121124
let val = port.to_ne_bytes();
122-
if let Err(e) = skel.maps_mut().ports().update(&key, &val, MapFlags::ANY) {
123-
bail!("Example limited to 10 ports: {e}");
124-
}
125+
let () = skel
126+
.maps_mut()
127+
.ports()
128+
.update(&key, &val, MapFlags::ANY)
129+
.context("Example limited to 10 ports")?;
125130
}
126131
ingress.create()?;
127132

libbpf-cargo/src/gen/btf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl<'s> GenBtf<'s> {
373373
}
374374
Err(e) => {
375375
if gen_impl_default || !t.is_struct {
376-
bail!("Could not construct a necessary Default Impl: {}", e);
376+
return Err(e.context("Could not construct a necessary Default Impl"));
377377
}
378378
}
379379
};

libbpf-cargo/src/gen/mod.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -963,13 +963,12 @@ pub fn gen_single(
963963
),
964964
};
965965

966-
if let Err(e) = gen_skel(debug, name, obj_file, output, rustfmt_path) {
967-
bail!(
968-
"Failed to generate skeleton for {}: {}",
966+
let () = gen_skel(debug, name, obj_file, output, rustfmt_path).with_context(|| {
967+
format!(
968+
"Failed to generate skeleton for {}",
969969
obj_file.to_string_lossy(),
970-
e
971-
);
972-
}
970+
)
971+
})?;
973972

974973
Ok(())
975974
}
@@ -999,20 +998,19 @@ fn gen_project(
999998
let mut skel_path = obj.path.clone();
1000999
skel_path.pop();
10011000

1002-
match gen_skel(
1001+
let () = gen_skel(
10031002
debug,
10041003
&obj.name,
10051004
obj_file_path.as_path(),
10061005
OutputDest::Directory(skel_path.as_path()),
10071006
rustfmt_path,
1008-
) {
1009-
Ok(_) => (),
1010-
Err(e) => bail!(
1011-
"Failed to generate skeleton for {}: {}",
1012-
obj.path.as_path().display(),
1013-
e
1014-
),
1015-
}
1007+
)
1008+
.with_context(|| {
1009+
format!(
1010+
"Failed to generate skeleton for {}",
1011+
obj.path.as_path().display()
1012+
)
1013+
})?;
10161014

10171015
match package_objs.get_mut(&obj.package) {
10181016
Some(v) => v.push(obj.clone()),
@@ -1023,9 +1021,8 @@ fn gen_project(
10231021
}
10241022

10251023
for (package, objs) in package_objs {
1026-
if let Err(e) = gen_mods(&objs, rustfmt_path) {
1027-
bail!("Failed to generate mod.rs for package={}: {}", package, e);
1028-
}
1024+
let () = gen_mods(&objs, rustfmt_path)
1025+
.with_context(|| format!("Failed to generate mod.rs for package={package}"))?;
10291026
}
10301027

10311028
Ok(())

libbpf-cargo/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
//! build`. This is a convenience command so you don't forget any steps. Alternatively, you could
5656
//! write a Makefile for your project.
5757
58+
#![allow(clippy::let_unit_value)]
5859
#![warn(
5960
elided_lifetimes_in_paths,
6061
single_use_lifetimes,

libbpf-cargo/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::let_unit_value)]
2+
13
use std::path::PathBuf;
24

35
use anyhow::Result;

libbpf-cargo/src/metadata.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::Path;
33
use std::path::PathBuf;
44

55
use anyhow::bail;
6+
use anyhow::Context as _;
67
use anyhow::Result;
78
use cargo_metadata::MetadataCommand;
89
use cargo_metadata::Package;
@@ -97,7 +98,7 @@ fn get_package(
9798
);
9899
}
99100
} else {
100-
bail!(e);
101+
return Err(e.into());
101102
}
102103
}
103104
};
@@ -148,11 +149,7 @@ pub fn get(debug: bool, manifest_path: Option<&PathBuf>) -> Result<(PathBuf, Vec
148149
cmd.manifest_path(path);
149150
}
150151

151-
let metadata = match cmd.exec() {
152-
Ok(m) => m,
153-
Err(e) => bail!("Failed to get cargo metadata: {}", e),
154-
};
155-
152+
let metadata = cmd.exec().context("Failed to get cargo metadata")?;
156153
if metadata.workspace_members.is_empty() {
157154
bail!("Failed to find targets")
158155
}
@@ -162,10 +159,9 @@ pub fn get(debug: bool, manifest_path: Option<&PathBuf>) -> Result<(PathBuf, Vec
162159
for id in &metadata.workspace_members {
163160
for package in &metadata.packages {
164161
if id == &package.id {
165-
match &mut get_package(debug, package, &target_directory) {
166-
Ok(vv) => v.append(vv),
167-
Err(e) => bail!("Failed to process package={}, error={}", package.name, e),
168-
}
162+
let vv = &mut get_package(debug, package, &target_directory)
163+
.with_context(|| format!("Failed to process package={}", package.name))?;
164+
let () = v.append(vv);
169165
}
170166
}
171167
}

0 commit comments

Comments
 (0)