Skip to content

Commit

Permalink
1. fix anyhow with_context loss other backtrace
Browse files Browse the repository at this point in the history
2.generate error also use anyhow::error
  • Loading branch information
taikulawo committed Jul 13, 2023
1 parent be363a4 commit 74d24f3
Showing 1 changed file with 13 additions and 12 deletions.
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(transparent)]
Build(anyhow::Error),
#[error(transparent)]
Generate(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

0 comments on commit 74d24f3

Please sign in to comment.