Skip to main content

nautilus_rs/
error.rs

1/// The top-level error type returned by every fallible SDK operation.
2#[derive(Debug, thiserror::Error)]
3pub enum Error {
4    /// A non-2xx response from the Verne API containing a structured error
5    /// body.
6    #[error("API error {}: {}", .0.status, .0.message)]
7    Api(#[from] ApiError),
8
9    /// A lower-level HTTP or networking failure (connection refused, timeout,
10    /// TLS error, …).
11    #[error("HTTP client error: {0}")]
12    Http(#[from] reqwest::Error),
13
14    /// The client was not configured correctly (e.g. a required API key was
15    /// not provided).
16    #[error("Configuration error: {0}")]
17    Config(String),
18
19    /// The response body could not be deserialised as the expected type.
20    #[error("JSON error: {0}")]
21    Json(#[from] serde_json::Error),
22}
23
24/// A structured error returned by the Verne API.
25///
26/// Returned inside [`Error::Api`] when the server responds with a non-2xx
27/// status and a JSON error envelope.
28#[derive(Debug, Clone)]
29pub struct ApiError {
30    /// Machine-readable error code (e.g. `"identity_not_found"`).
31    pub code: String,
32    /// Human-readable description of the error.
33    pub message: String,
34    /// HTTP status code (e.g. `404`, `422`).
35    pub status: u16,
36    /// Unique request identifier for support/debugging.
37    pub request_id: String,
38}
39
40impl std::fmt::Display for ApiError {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(
43            f,
44            "[{}] {} (request_id: {})",
45            self.code, self.message, self.request_id
46        )
47    }
48}
49
50impl std::error::Error for ApiError {}