Skip to main content

codex/
errors.rs

1use thiserror::Error;
2
3/// A specialized `Result` type for Codex SDK operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Unified error type for all Codex SDK operations.
7#[derive(Debug, Error)]
8pub enum Error {
9    /// The `codex` executable could not be resolved.
10    #[error("Codex CLI not found: {0}")]
11    CliNotFound(String),
12
13    /// Spawning the Codex subprocess failed.
14    #[error("Failed to spawn Codex CLI: {0}")]
15    Spawn(String),
16
17    /// The Codex subprocess exited unsuccessfully.
18    #[error("Codex process exited with {detail}: {stderr}")]
19    Process {
20        /// Human-readable exit detail such as `code 2` or signal termination.
21        detail: String,
22        /// Full stderr output captured from the Codex subprocess.
23        stderr: String,
24        /// Numeric exit code when available.
25        code: Option<i32>,
26    },
27
28    /// A line from stdout could not be decoded as JSON.
29    #[error("Failed to parse JSON event: {0}")]
30    JsonParse(String),
31
32    /// A thread turn failed according to Codex turn-level error events.
33    #[error("Thread run failed: {0}")]
34    ThreadRun(String),
35
36    /// The provided output schema is invalid.
37    #[error("Invalid output schema: {0}")]
38    InvalidOutputSchema(String),
39
40    /// A `config` override cannot be represented as Codex CLI TOML literal flags.
41    #[error("Invalid config override: {0}")]
42    InvalidConfig(String),
43
44    /// The caller cancelled an in-flight turn.
45    #[error("Turn cancelled")]
46    Cancelled,
47
48    /// Wrapper for I/O errors while interacting with the subprocess or temp files.
49    #[error(transparent)]
50    Io(#[from] std::io::Error),
51
52    /// Wrapper for JSON serialization/deserialization errors.
53    #[error(transparent)]
54    Json(#[from] serde_json::Error),
55}