Skip to main content

oci_rust_sdk/core/
error.rs

1/// Result type alias for OCI operations
2pub type Result<T> = std::result::Result<T, OciError>;
3
4/// Main error type for OCI SDK operations
5#[derive(Debug, thiserror::Error)]
6pub enum OciError {
7    /// HTTP request failed
8    #[error("HTTP request failed: {0}")]
9    HttpError(#[from] reqwest::Error),
10
11    /// Authentication failed
12    #[error("Authentication failed: {0}")]
13    AuthError(String),
14
15    /// Serialization/deserialization error
16    #[error("Serialization error: {0}")]
17    SerdeError(#[from] serde_json::Error),
18
19    /// Service returned an error
20    #[error("Service error {status}: {message}")]
21    ServiceError {
22        status: u16,
23        code: String,
24        message: String,
25    },
26
27    /// Configuration error
28    #[error("Configuration error: {0}")]
29    ConfigError(String),
30
31    /// I/O error
32    #[error("I/O error: {0}")]
33    IoError(#[from] std::io::Error),
34
35    /// Invalid region
36    #[error("Invalid region: {0}")]
37    InvalidRegion(String),
38
39    /// RSA signing error
40    #[error("RSA signing error: {0}")]
41    SigningError(String),
42
43    /// Other errors
44    #[error("{0}")]
45    Other(String),
46}
47
48impl OciError {
49    /// Check if this error is retryable
50    pub fn is_retryable(&self) -> bool {
51        match self {
52            // 429 Too Many Requests - retryable
53            OciError::ServiceError { status: 429, .. } => true,
54            // 5xx Server Errors - retryable
55            OciError::ServiceError { status, .. } if *status >= 500 => true,
56            // Network errors - retryable
57            OciError::HttpError(_) => true,
58            // Everything else is not retryable
59            _ => false,
60        }
61    }
62
63    /// Create a service error from response
64    pub fn from_response(status: u16, code: String, message: String) -> Self {
65        OciError::ServiceError {
66            status,
67            code,
68            message,
69        }
70    }
71}
72
73/// Error response from OCI services
74#[derive(Debug, serde::Deserialize)]
75pub struct ServiceErrorResponse {
76    pub code: String,
77    pub message: String,
78}