Skip to main content

claude_codes/
error.rs

1//! Error types for the Claude Code protocol
2
3use crate::io::ParseError;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("JSON serialization error: {0}")]
9    Json(#[from] serde_json::Error),
10
11    #[error("IO error: {0}")]
12    Io(#[from] std::io::Error),
13
14    #[error("Protocol error: {0}")]
15    Protocol(String),
16
17    #[error("Invalid message type: {0}")]
18    InvalidMessageType(String),
19
20    #[error("Missing required field: {0}")]
21    MissingField(String),
22
23    #[error("Invalid state transition: {0}")]
24    InvalidState(String),
25
26    #[error("Authorization code rejected: {message}")]
27    CodeRejected { message: String },
28
29    #[error(
30        "Timed out awaiting a login outcome; CLI screen content after submission:\n{transcript}"
31    )]
32    LoginTimeout { transcript: String },
33
34    /// The login CLI process exited without producing a login outcome.
35    ///
36    /// Reading `code` (signal deaths surface as `128 + signo`):
37    /// - `0` / small integers — the CLI exited on its own (its error text,
38    ///   if any, is in `transcript`).
39    /// - `143` (SIGTERM) — terminated externally, OR the host application
40    ///   dropped the `LoginFlow` mid-flight (the crate's `Drop` kills with
41    ///   SIGTERM and, with the `log` feature, warns
42    ///   "LoginFlow dropped while unfinished" — check logs to attribute).
43    /// - `137` (SIGKILL) — killed externally (OOM killer, `kill -9`); the
44    ///   crate never sends SIGKILL.
45    #[error("Login CLI exited (code {code:?}) without a login outcome; output:\n{transcript}")]
46    LoginChildExited {
47        code: Option<u32>,
48        transcript: String,
49    },
50
51    /// The login CLI exited cleanly (code 0) without an observed credentials
52    /// write, and `claude auth status` did not confirm a login of the flow's
53    /// kind. Not proof the login failed — completion just couldn't be
54    /// verified.
55    ///
56    /// - `logged_in` / `auth_method` — what the status reported; `None` when
57    ///   the status check itself failed (see `status_error`). `logged_in:
58    ///   Some(false)` means signed out; `Some(true)` with another
59    ///   `auth_method` means a different credential kind is active.
60    /// - `status_error` — why the status check failed (spawn error, timeout,
61    ///   unparseable output).
62    #[error(
63        "Login CLI exited cleanly but the login could not be verified (logged_in: {logged_in:?}, auth_method: {auth_method:?}, status_error: {status_error:?}); output:\n{transcript}"
64    )]
65    LoginExitUnverified {
66        logged_in: Option<bool>,
67        auth_method: Option<String>,
68        status_error: Option<String>,
69        transcript: String,
70    },
71
72    #[error("Timeout occurred")]
73    Timeout,
74
75    #[error("Connection closed")]
76    ConnectionClosed,
77
78    #[error("Deserialization error: {0}")]
79    Deserialization(#[from] ParseError),
80
81    #[error("Session UUID not yet available - no response received")]
82    SessionNotInitialized,
83
84    #[error("Binary not found: '{name}' is not on PATH. Is it installed?")]
85    BinaryNotFound { name: String },
86
87    #[error("Unknown error: {0}")]
88    Unknown(String),
89}
90
91pub type Result<T> = std::result::Result<T, Error>;