codex_codes/error.rs
1//! Error types for the codex-codes crate.
2//!
3//! All fallible operations return [`Result<T>`], which uses [`enum@Error`] as the
4//! error type. The variants cover JSON serialization, I/O, protocol-level
5//! issues, and JSON-RPC errors from the app-server.
6
7use thiserror::Error;
8
9/// All possible errors from codex-codes operations.
10#[derive(Error, Debug)]
11pub enum Error {
12 /// JSON serialization or deserialization failed.
13 ///
14 /// Returned when request parameters can't be serialized or
15 /// response payloads don't match expected types.
16 #[error("JSON error: {0}")]
17 Json(#[from] serde_json::Error),
18
19 /// An I/O error occurred communicating with the app-server process.
20 ///
21 /// Common causes: process not found, pipe broken, permission denied.
22 #[error("IO error: {0}")]
23 Io(#[from] std::io::Error),
24
25 /// A protocol-level error (e.g., missing stdin/stdout pipes).
26 #[error("Protocol error: {0}")]
27 Protocol(String),
28
29 /// The app-server connection was closed unexpectedly.
30 #[error("Connection closed")]
31 ConnectionClosed,
32
33 /// A message from the server could not be deserialized.
34 ///
35 /// Includes the raw message text for debugging. If you encounter this,
36 /// please report it — it likely indicates a protocol change.
37 #[error("Deserialization error: {0}")]
38 Deserialization(String),
39
40 /// The app-server process exited with a non-zero status.
41 #[error("Process exited with status {0}: {1}")]
42 ProcessFailed(i32, String),
43
44 /// The server returned a JSON-RPC error response.
45 ///
46 /// Contains the error code and message from the server.
47 /// See the Codex CLI docs for error code meanings.
48 #[error("JSON-RPC error ({code}): {message}")]
49 JsonRpc { code: i64, message: String },
50
51 /// The server closed the connection (EOF on stdout).
52 ///
53 /// Returned by `request()` if the server exits mid-conversation.
54 #[error("Server closed connection")]
55 ServerClosed,
56
57 /// An unclassified error.
58 #[error("Unknown error: {0}")]
59 Unknown(String),
60}
61
62/// A `Result` type alias using [`enum@Error`].
63pub type Result<T> = std::result::Result<T, Error>;