sqlite_graphrag/spawn/
error_propagator.rs1use crate::errors::AppError;
4use std::process::Output;
5
6pub struct ErrorPropagator {
10 pub binary: String,
12 pub args: Vec<String>,
14}
15
16impl ErrorPropagator {
17 pub fn new(binary: impl Into<String>, args: Vec<String>) -> Self {
19 Self {
20 binary: binary.into(),
21 args,
22 }
23 }
24
25 pub fn propagate(&self, output: &Output) -> Result<(), AppError> {
27 if output.status.success() {
28 return Ok(());
29 }
30 let stderr = String::from_utf8_lossy(&output.stderr);
31 let stdout = String::from_utf8_lossy(&output.stdout);
32 let code = output.status.code().unwrap_or(-1);
33 let mut msg = format!("{} exited with code {}", self.binary, code);
34 if !stderr.trim().is_empty() {
35 msg.push_str(&format!("\nstderr: {}", stderr.trim()));
36 }
37 if !stdout.trim().is_empty() {
38 msg.push_str(&format!("\nstdout: {}", stdout.trim()));
39 }
40 msg.push_str(&format!("\nargs: {}", self.args.join(" ")));
41 Err(AppError::Internal(anyhow::anyhow!(msg)))
42 }
43
44 pub fn require_success(&self, output: &Output) -> Result<String, AppError> {
46 self.propagate(output)?;
47 Ok(String::from_utf8_lossy(&output.stdout).into_owned())
48 }
49}