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    #[error("Timeout occurred")]
52    Timeout,
53
54    #[error("Connection closed")]
55    ConnectionClosed,
56
57    #[error("Deserialization error: {0}")]
58    Deserialization(#[from] ParseError),
59
60    #[error("Session UUID not yet available - no response received")]
61    SessionNotInitialized,
62
63    #[error("Binary not found: '{name}' is not on PATH. Is it installed?")]
64    BinaryNotFound { name: String },
65
66    #[error("Unknown error: {0}")]
67    Unknown(String),
68}
69
70pub type Result<T> = std::result::Result<T, Error>;