Skip to content
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

fix anyhow with_context loss other backtrace #513

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 13 additions & 12 deletions libbpf-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
)]
#![deny(unsafe_op_in_unsafe_fn)]

use anyhow::anyhow;
use std::path::Path;
use std::path::PathBuf;
use std::result;
Expand All @@ -87,10 +88,10 @@ mod test;
/// Canonical error type for this crate.
#[derive(Error, Debug)]
pub enum Error {
#[error("Error building BPF object file: {0}")]
Build(String),
#[error("Error generating skeleton: {0}")]
Generate(String),
#[error("Error building BPF object file")]
Build(#[source] anyhow::Error),
#[error("Error generating skeleton")]
Generate(#[source] anyhow::Error),
}

pub type Result<T> = result::Result<T, Error>;
Expand Down Expand Up @@ -223,24 +224,24 @@ impl SkeletonBuilder {
let source = self
.source
.as_ref()
.ok_or_else(|| Error::Build("No source file".into()))?;
.ok_or_else(|| Error::Build(anyhow!("No source file")))?;

let filename = source
.file_name()
.ok_or_else(|| Error::Build("Missing file name".into()))?
.ok_or_else(|| Error::Build(anyhow!("Missing file name")))?
.to_str()
.ok_or_else(|| Error::Build("Invalid unicode in file name".into()))?;
.ok_or_else(|| Error::Build(anyhow!("Invalid unicode in file name")))?;

if !filename.ends_with(".bpf.c") {
return Err(Error::Build(format!(
return Err(Error::Build(anyhow!(
"Source file={} does not have .bpf.c suffix",
source.display()
)));
}

if self.obj.is_none() {
let name = filename.split('.').next().unwrap();
let dir = tempdir().map_err(|e| Error::Build(e.to_string()))?;
let dir = tempdir().map_err(|e| Error::Build(e.into()))?;
let objfile = dir.path().join(format!("{name}.o"));
self.obj = Some(objfile);
// Hold onto tempdir so that it doesn't get deleted early
Expand All @@ -256,7 +257,7 @@ impl SkeletonBuilder {
self.skip_clang_version_check,
&self.clang_args,
)
.map_err(|e| Error::Build(e.to_string()))?;
.map_err(Error::Build)?;

Ok(())
}
Expand All @@ -268,15 +269,15 @@ impl SkeletonBuilder {
let objfile = self
.obj
.as_ref()
.ok_or_else(|| Error::Generate("No object file".into()))?;
.ok_or_else(|| Error::Generate(anyhow!("No object file")))?;

gen::gen_single(
self.debug,
objfile,
gen::OutputDest::File(output.as_ref()),
Some(&self.rustfmt),
)
.map_err(|e| Error::Generate(e.to_string()))?;
.map_err(Error::Generate)?;

Ok(())
}
Expand Down