Skip to main content

kimi_wire/
error.rs

1use std::time::Duration;
2
3/// Errors that can occur when interacting with the Kimi Wire protocol.
4#[derive(thiserror::Error, Debug, Clone, PartialEq)]
5pub enum WireError {
6    /// The wire stream closed unexpectedly.
7    #[error("wire stream closed")]
8    StreamClosed,
9
10    /// A read or write operation timed out.
11    #[error("wire I/O timed out after {0:?}")]
12    Timeout(Duration),
13
14    /// Failed to spawn the Kimi CLI process.
15    #[error("failed to spawn process: {0}")]
16    SpawnFailed(String),
17
18    /// Failed to parse a JSON message.
19    #[error("JSON parse error: {0}")]
20    JsonParse(String),
21
22    /// Failed to serialize a value to JSON.
23    #[error("JSON serialization error: {0}")]
24    JsonSerialize(String),
25
26    /// The server returned a JSON-RPC error response.
27    /// The server returned a JSON-RPC error response.
28    #[error("wire request failed: {message} (code: {code})")]
29    RequestFailed {
30        /// JSON-RPC error code.
31        code: i32,
32        /// Error message from the server.
33        message: String,
34    },
35
36    /// Received a response with an unexpected request id.
37    /// Received a response with an unexpected request id.
38    #[error("unexpected response id: expected {expected}, got {got}")]
39    UnexpectedResponseId {
40        /// Expected request id.
41        expected: String,
42        /// Actual request id received.
43        got: String,
44    },
45
46    /// The server does not support the requested method.
47    #[error("method not found: {0}")]
48    MethodNotFound(String),
49
50    /// An unknown wire message type was received.
51    #[error("unknown wire message type: {0}")]
52    UnknownMessageType(String),
53
54    /// The payload was not a JSON object.
55    #[error("wire message payload must be a JSON object")]
56    InvalidPayloadType,
57
58    /// An I/O error occurred.
59    #[error("I/O error: {0}")]
60    Io(String),
61
62    /// A generic internal error.
63    #[error("internal error: {0}")]
64    Internal(String),
65}
66
67impl From<std::io::Error> for WireError {
68    fn from(err: std::io::Error) -> Self {
69        WireError::Io(err.to_string())
70    }
71}
72
73impl From<serde_json::Error> for WireError {
74    fn from(err: serde_json::Error) -> Self {
75        if err.is_io() {
76            WireError::Io(err.to_string())
77        } else {
78            WireError::JsonParse(err.to_string())
79        }
80    }
81}