Skip to main content

kimberlite_client/
error.rs

1//! Client error types.
2
3use kimberlite_wire::{ErrorCode, WireError};
4use thiserror::Error;
5
6/// Result type for client operations.
7pub type ClientResult<T> = Result<T, ClientError>;
8
9/// Errors that can occur during client operations.
10#[derive(Debug, Error)]
11pub enum ClientError {
12    /// Connection error.
13    #[error("connection error: {0}")]
14    Connection(#[from] std::io::Error),
15
16    /// Wire protocol error.
17    #[error("wire protocol error: {0}")]
18    Wire(#[from] WireError),
19
20    /// Server returned an error response.
21    #[error("server error ({code:?}): {message}")]
22    Server { code: ErrorCode, message: String },
23
24    /// Connection not established.
25    #[error("not connected to server")]
26    NotConnected,
27
28    /// Response ID mismatch.
29    #[error("response ID {received} does not match request ID {expected}")]
30    ResponseMismatch { expected: u64, received: u64 },
31
32    /// Unexpected response type.
33    #[error("unexpected response type: expected {expected}, got {actual}")]
34    UnexpectedResponse { expected: String, actual: String },
35
36    /// Connection timeout.
37    #[error("connection timeout")]
38    Timeout,
39
40    /// Handshake failed.
41    #[error("handshake failed: {0}")]
42    HandshakeFailed(String),
43}
44
45impl ClientError {
46    /// Creates a server error from an error code and message.
47    pub fn server(code: ErrorCode, message: impl Into<String>) -> Self {
48        Self::Server {
49            code,
50            message: message.into(),
51        }
52    }
53}