Skip to main content

harness/
error.rs

1use std::path::PathBuf;
2
3/// All errors that can occur in the harness.
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    #[error("agent binary not found: {binary} (is {agent} installed?)")]
7    BinaryNotFound { agent: String, binary: String },
8
9    #[error("failed to spawn agent process: {0}")]
10    SpawnFailed(#[source] std::io::Error),
11
12    #[error("agent process failed with exit code {code}: {stderr}")]
13    ProcessFailed { code: i32, stderr: String },
14
15    #[error("failed to parse agent output: {0}")]
16    ParseError(String),
17
18    #[error("agent timed out after {0} seconds")]
19    Timeout(u64),
20
21    #[error("working directory does not exist: {0}")]
22    InvalidWorkDir(PathBuf),
23
24    #[error("I/O error: {0}")]
25    Io(#[from] std::io::Error),
26
27    #[error("JSON error: {0}")]
28    Json(#[from] serde_json::Error),
29
30    #[error("failed to parse models registry: {0}")]
31    ModelsParse(String),
32
33    #[error("failed to fetch models registry: {0}")]
34    ModelsFetch(String),
35
36    #[error("{0}")]
37    Other(String),
38}
39
40impl Error {
41    /// Stable error code string for programmatic consumption.
42    pub fn code(&self) -> &'static str {
43        match self {
44            Error::BinaryNotFound { .. } => "E001",
45            Error::SpawnFailed(_) => "E002",
46            Error::ProcessFailed { .. } => "E003",
47            Error::ParseError(_) => "E004",
48            Error::Timeout(_) => "E005",
49            Error::InvalidWorkDir(_) => "E006",
50            Error::Io(_) => "E007",
51            Error::Json(_) => "E008",
52            Error::ModelsParse(_) => "E010",
53            Error::ModelsFetch(_) => "E011",
54            Error::Other(_) => "E999",
55        }
56    }
57}
58
59pub type Result<T> = std::result::Result<T, Error>;