Skip to main content

orpc_client/
error.rs

1use std::fmt;
2
3use orpc::ORPCError;
4
5/// Unified error type for the oRPC client.
6///
7/// Covers transport errors (network), wire protocol errors (bad JSON),
8/// and application-level RPC errors from the server.
9#[derive(Debug)]
10pub enum ClientError {
11    /// HTTP transport error (network, DNS, TLS, timeout).
12    Transport(reqwest::Error),
13    /// Failed to serialize the request input.
14    Serialize(serde_json::Error),
15    /// Failed to deserialize the response body.
16    Deserialize(serde_json::Error),
17    /// Server returned an oRPC error response (4xx/5xx).
18    Rpc(ORPCError),
19    /// SSE stream protocol error (malformed event, unexpected format).
20    Sse(String),
21}
22
23impl fmt::Display for ClientError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            ClientError::Transport(e) => write!(f, "transport error: {e}"),
27            ClientError::Serialize(e) => write!(f, "serialize error: {e}"),
28            ClientError::Deserialize(e) => write!(f, "deserialize error: {e}"),
29            ClientError::Rpc(e) => write!(f, "rpc error: {e}"),
30            ClientError::Sse(msg) => write!(f, "sse error: {msg}"),
31        }
32    }
33}
34
35impl std::error::Error for ClientError {
36    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37        match self {
38            ClientError::Transport(e) => Some(e),
39            ClientError::Serialize(e) => Some(e),
40            ClientError::Deserialize(e) => Some(e),
41            ClientError::Rpc(e) => Some(e),
42            _ => None,
43        }
44    }
45}
46
47impl From<reqwest::Error> for ClientError {
48    fn from(err: reqwest::Error) -> Self {
49        ClientError::Transport(err)
50    }
51}
52
53impl From<ORPCError> for ClientError {
54    fn from(err: ORPCError) -> Self {
55        ClientError::Rpc(err)
56    }
57}