Skip to main content

cortexa_aws_errors/
kind.rs

1//! Error categories, retryability classification, and structured AWS service error details.
2
3use std::borrow::Cow;
4use std::time::Duration;
5
6/// High-level classification of an AWS SDK failure.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum AwsErrorKind {
10    /// Input validation or invariant check failed before sending a request.
11    Validation,
12    /// Credentials could not be resolved or were invalid.
13    Credentials,
14    /// Request signing (SigV4) failed.
15    Signing,
16    /// Underlying HTTP or network transport failure.
17    Transport,
18    /// Request or operation exceeded its configured timeout.
19    Timeout,
20    /// Request payload could not be serialized.
21    Serialization,
22    /// Response payload could not be deserialized or exceeded safety bounds.
23    Deserialization,
24    /// AWS service returned a structured error response.
25    Service,
26    /// AWS service throttled the request (e.g. HTTP 429 or `Throttling`).
27    Throttled,
28    /// Requested AWS resource or item was not found.
29    NotFound,
30    /// Resource creation failed because the resource already exists.
31    AlreadyExists,
32    /// A waiter exceeded its maximum attempts or timeout budget.
33    WaiterTimeout,
34    /// A waiter was cancelled before completion.
35    WaiterCancelled,
36    /// A batch operation completed with one or more failed items.
37    BatchPartialFailure,
38}
39
40/// Indicates whether a failed operation can safely be retried.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub enum Retryability {
43    /// The operation may succeed on retry, optionally honoring a server-provided delay.
44    Retryable {
45        /// Optional server-requested delay (such as HTTP `Retry-After`).
46        retry_after: Option<Duration>,
47    },
48    /// The error is terminal for the current inputs or state.
49    NonRetryable,
50}
51
52impl Retryability {
53    /// Returns `true` when the failure is retryable.
54    #[must_use]
55    pub const fn is_retryable(self) -> bool {
56        matches!(self, Self::Retryable { .. })
57    }
58
59    /// Returns any explicit retry delay hint, if present.
60    #[must_use]
61    pub const fn retry_after(self) -> Option<Duration> {
62        match self {
63            Self::Retryable { retry_after } => retry_after,
64            Self::NonRetryable => None,
65        }
66    }
67}
68
69/// Metadata returned by an AWS service in an error response.
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct ServiceErrorDetails {
72    service: Cow<'static, str>,
73    operation: Cow<'static, str>,
74    code: String,
75    message: String,
76    http_status: u16,
77    request_id: Option<String>,
78}
79
80impl ServiceErrorDetails {
81    /// Constructs structured AWS service error details.
82    #[must_use]
83    pub fn new(
84        service: impl Into<Cow<'static, str>>,
85        operation: impl Into<Cow<'static, str>>,
86        code: impl Into<String>,
87        message: impl Into<String>,
88        http_status: u16,
89        request_id: Option<String>,
90    ) -> Self {
91        Self {
92            service: service.into(),
93            operation: operation.into(),
94            code: code.into(),
95            message: message.into(),
96            http_status,
97            request_id,
98        }
99    }
100
101    /// Returns the AWS service identifier (e.g. `"s3"`, `"dynamodb"`, `"ec2"`).
102    #[must_use]
103    pub fn service(&self) -> &str {
104        &self.service
105    }
106
107    /// Returns the AWS API operation name (e.g. `"HeadBucket"`).
108    #[must_use]
109    pub fn operation(&self) -> &str {
110        &self.operation
111    }
112
113    /// Returns the AWS error code returned by the service.
114    #[must_use]
115    pub fn code(&self) -> &str {
116        &self.code
117    }
118
119    /// Returns the human-readable error message from the service.
120    #[must_use]
121    pub fn message(&self) -> &str {
122        &self.message
123    }
124
125    /// Returns the HTTP status code associated with the service error.
126    #[must_use]
127    pub const fn http_status(&self) -> u16 {
128        self.http_status
129    }
130
131    /// Returns the AWS request identifier when provided in headers or body.
132    #[must_use]
133    pub fn request_id(&self) -> Option<&str> {
134        self.request_id.as_deref()
135    }
136}