1use std::process::Output;
2use thiserror::Error;
3
4pub type Result<T> = std::result::Result<T, GhError>;
6
7#[derive(Error, Debug)]
9pub enum GhError {
10 #[error("GitHub CLI is not installed or not found in PATH")]
11 GhNotFound,
12
13 #[error("Command execution failed: {0}")]
14 ExecutionFailed(String),
15
16 #[error("Command failed with exit code {code}: {stderr}")]
17 CommandFailed { code: i32, stderr: String },
18
19 #[error("IO error: {0}")]
20 IoError(#[from] std::io::Error),
21
22 #[error("UTF-8 conversion error: {0}")]
23 Utf8Error(#[from] std::string::FromUtf8Error),
24
25 #[error("Invalid command: {0}")]
26 InvalidCommand(String),
27}
28
29impl GhError {
30 pub fn from_output(output: Output) -> Self {
32 let stderr = String::from_utf8_lossy(&output.stderr).to_string();
33 let code = output.status.code().unwrap_or(-1);
34 GhError::CommandFailed { code, stderr }
35 }
36}