Skip to main content

monoloop_connector_codex/
error.rs

1//! Codex connector errors (bounded, no secrets/bodies).
2
3use monoloop_contracts::{ConnectorError, ConnectorErrorKind};
4
5/// Codex / codex connector failure.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct CodexConnectorError {
8    /// Closed kind.
9    pub kind: ConnectorErrorKind,
10    /// Safe message.
11    pub message: String,
12}
13
14impl CodexConnectorError {
15    /// Configuration invalid.
16    pub fn configuration(msg: impl Into<String>) -> Self {
17        Self {
18            kind: ConnectorErrorKind::ConfigurationInvalid,
19            message: msg.into(),
20        }
21    }
22
23    /// Process spawn / I/O failure.
24    pub fn connection(msg: impl Into<String>) -> Self {
25        Self {
26            kind: ConnectorErrorKind::ConnectionFailed,
27            message: msg.into(),
28        }
29    }
30
31    /// Protocol / dialect framing failure.
32    pub fn protocol(msg: impl Into<String>) -> Self {
33        Self {
34            kind: ConnectorErrorKind::ProtocolFailed,
35            message: msg.into(),
36        }
37    }
38
39    /// Deadline exceeded.
40    pub fn deadline(msg: impl Into<String>) -> Self {
41        Self {
42            kind: ConnectorErrorKind::DeadlineExceeded,
43            message: msg.into(),
44        }
45    }
46
47    /// Session create/load/prompt failure.
48    pub fn session(msg: impl Into<String>) -> Self {
49        Self {
50            kind: ConnectorErrorKind::SessionFailed,
51            message: msg.into(),
52        }
53    }
54
55    /// Cancelled.
56    pub fn cancelled() -> Self {
57        Self {
58            kind: ConnectorErrorKind::Cancelled,
59            message: "codex session cancelled".into(),
60        }
61    }
62
63    /// Map to contracts ConnectorError.
64    pub fn into_connector_error(self) -> ConnectorError {
65        ConnectorError::new(self.kind, self.message)
66    }
67}
68
69impl From<CodexConnectorError> for ConnectorError {
70    fn from(e: CodexConnectorError) -> Self {
71        e.into_connector_error()
72    }
73}
74
75impl std::fmt::Display for CodexConnectorError {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        write!(f, "{:?}: {}", self.kind, self.message)
78    }
79}
80
81impl std::error::Error for CodexConnectorError {}