From 9bc185a61e53ccc51b023c3023d45fcccff70358 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 8 Oct 2025 08:03:30 -0700 Subject: [PATCH] aya-tool: simplify and improve error handling --- aya-tool/src/generate.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/aya-tool/src/generate.rs b/aya-tool/src/generate.rs index 2a5b0993..815fbf2a 100644 --- a/aya-tool/src/generate.rs +++ b/aya-tool/src/generate.rs @@ -2,7 +2,7 @@ use std::{ fs::{self, File}, io::{self, Write as _}, path::{Path, PathBuf}, - process::Command, + process::{Command, Output}, str, }; @@ -14,14 +14,14 @@ pub enum Error { #[error("error executing bpftool")] BpfTool(#[source] io::Error), - #[error("{stderr}\nbpftool failed with exit code {code}")] - BpfToolExit { code: i32, stderr: String }, + #[error("bpftool failed: {0:?}")] + BpfToolExit(Output), #[error("bindgen failed")] Bindgen(#[source] io::Error), - #[error("{stderr}\nbindgen failed with exit code {code}")] - BindgenExit { code: i32, stderr: String }, + #[error("bindgen failed: {0:?}")] + BindgenExit(Output), #[error("error reading header file")] ReadHeaderFile(#[source] io::Error), @@ -74,14 +74,13 @@ pub fn generate>( .output() .map_err(Error::Bindgen)?; - if !output.status.success() { - return Err(Error::BindgenExit { - code: output.status.code().unwrap(), - stderr: str::from_utf8(&output.stderr).unwrap().to_owned(), - }); + let Output { status, .. } = &output; + if !status.success() { + return Err(Error::BindgenExit(output)); } + let Output { stdout, .. } = output; - Ok(str::from_utf8(&output.stdout).unwrap().to_owned()) + Ok(String::from_utf8(stdout).unwrap()) } fn c_header_from_btf(path: &Path) -> Result { @@ -92,14 +91,13 @@ fn c_header_from_btf(path: &Path) -> Result { .output() .map_err(Error::BpfTool)?; - if !output.status.success() { - return Err(Error::BpfToolExit { - code: output.status.code().unwrap(), - stderr: str::from_utf8(&output.stderr).unwrap().to_owned(), - }); + let Output { status, .. } = &output; + if !status.success() { + return Err(Error::BpfToolExit(output)); } + let Output { stdout, .. } = output; - Ok(str::from_utf8(&output.stdout).unwrap().to_owned()) + Ok(String::from_utf8(stdout).unwrap()) } fn extract_ctypes_prefix(s: &[String]) -> (Vec, Option) {