Skip to main content

iicp_client/
errors.rs

1// SPDX-License-Identifier: Apache-2.0
2use thiserror::Error;
3
4/// All errors emitted by the IICP client SDK.
5#[derive(Debug, Error)]
6pub enum IicpError {
7    /// Network or HTTP transport failure.
8    #[error("network error: {0}")]
9    Http(#[from] reqwest::Error),
10
11    /// Directory or node returned an IICP error response.
12    #[error("[{code}] {message} (HTTP {status})")]
13    Protocol {
14        code: String,
15        message: String,
16        status: u16,
17    },
18
19    /// SDK-03: intent URN does not match the required pattern.
20    #[error("SDK-03: invalid intent URN: {0}")]
21    InvalidIntent(String),
22
23    /// SDK-04: timeout_ms exceeds the maximum of 120 000 ms.
24    #[error("SDK-04: timeout_ms must be ≤ 120000; got {0}")]
25    TimeoutTooLarge(u64),
26
27    /// Discover returned an empty node list.
28    #[error("no nodes available for intent {intent}")]
29    NoNodes { intent: String },
30
31    /// JSON serialization / deserialization failure.
32    #[error("serialization error: {0}")]
33    Serde(#[from] serde_json::Error),
34
35    /// Node registration, heartbeat, or server bind failure.
36    #[error("node error: {0}")]
37    Node(String),
38}
39
40impl IicpError {
41    /// True for errors where retrying a different attempt may succeed (SDK-05).
42    pub fn is_transient(&self) -> bool {
43        matches!(
44            self,
45            IicpError::Protocol {
46                status: 429 | 502 | 503 | 504,
47                ..
48            } | IicpError::Http(_)
49        )
50    }
51}
52
53pub type Result<T> = std::result::Result<T, IicpError>;