Skip to main content

bankr_agent_api/
error.rs

1//! Error types for the `bankr-agent-api` crate.
2
3use crate::types::ApiErrorBody;
4
5/// Errors that can occur when interacting with the Bankr Agent API.
6#[derive(Debug, thiserror::Error)]
7pub enum BankrError {
8    /// HTTP transport error from hpx-transport.
9    #[error("HTTP transport error: {0}")]
10    Transport(String),
11
12    /// The API returned a non-success HTTP status code.
13    #[error("API error (HTTP {status}): {body}")]
14    Api {
15        /// HTTP status code.
16        status: u16,
17        /// Parsed error body (if available).
18        body: ApiErrorBody,
19    },
20
21    /// Failed to deserialize the API response.
22    #[error("Deserialization error: {0}")]
23    Deserialization(String),
24
25    /// Job polling timed out.
26    #[error("Job polling timed out after {attempts} attempts")]
27    PollTimeout {
28        /// Number of poll attempts made.
29        attempts: u32,
30    },
31
32    /// Job failed on the server side.
33    #[error("Job failed: {message}")]
34    JobFailed {
35        /// Error message from the API.
36        message: String,
37    },
38
39    /// Job was cancelled.
40    #[error("Job was cancelled")]
41    JobCancelled,
42
43    /// Client configuration error.
44    #[error("Configuration error: {0}")]
45    Config(String),
46}
47
48impl std::fmt::Display for ApiErrorBody {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        if let Some(ref msg) = self.message {
51            write!(f, "{msg}")
52        } else if let Some(ref err) = self.error {
53            write!(f, "{err}")
54        } else {
55            write!(f, "(no details)")
56        }
57    }
58}