Skip to main content

kcode_codex_runtime_v2/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// Stable classification for a runtime failure.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum ErrorKind {
6    /// The caller supplied an invalid request or response.
7    InvalidInput,
8    /// The Codex executable could not be started or contacted.
9    Unavailable,
10    /// Codex is not authenticated with ChatGPT.
11    Authentication,
12    /// The operation exceeded its deadline.
13    Timeout,
14    /// Codex returned an invalid or unsuccessful protocol message.
15    Protocol,
16    /// The caller stopped the turn.
17    Cancelled,
18}
19
20/// One safe runtime error.
21#[derive(Clone, Debug, Eq, PartialEq)]
22pub struct Error {
23    kind: ErrorKind,
24    message: String,
25}
26
27impl Error {
28    pub(crate) fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
29        Self {
30            kind,
31            message: message.into(),
32        }
33    }
34
35    /// Returns the stable error classification.
36    pub const fn kind(&self) -> ErrorKind {
37        self.kind
38    }
39
40    /// Returns the safe diagnostic message.
41    pub fn message(&self) -> &str {
42        &self.message
43    }
44}
45
46impl Display for Error {
47    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
48        formatter.write_str(&self.message)
49    }
50}
51
52impl std::error::Error for Error {}
53
54/// Result returned by this crate.
55pub type Result<T> = std::result::Result<T, Error>;