Skip to main content

cortexa_aws_errors/
error.rs

1//! Pointer-sized [`AwsError`] type, constructors, and boundary redaction mutators.
2
3use std::borrow::Cow;
4use std::time::Duration;
5
6use crate::kind::{AwsErrorKind, Retryability, ServiceErrorDetails};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub(crate) struct AwsErrorInner {
10    pub(crate) kind: AwsErrorKind,
11    pub(crate) message: Cow<'static, str>,
12    pub(crate) correlation_id: Cow<'static, str>,
13    pub(crate) service_error: Option<ServiceErrorDetails>,
14    pub(crate) retryability: Retryability,
15    pub(crate) details: Option<Cow<'static, str>>,
16}
17
18/// Compact pointer-sized error type (`size_of::<AwsError>() == size_of::<usize>()`).
19///
20/// Boxing the inner payload keeps `Result<T, AwsError>` and `async` future state
21/// machines minimal on the stack on the happy path.
22#[derive(Debug, Clone, PartialEq, Eq)]
23#[must_use]
24pub struct AwsError(pub(crate) Box<AwsErrorInner>);
25
26impl AwsError {
27    /// Creates an `AwsError` with full control over classification, retryability, and details.
28    pub fn new(
29        kind: AwsErrorKind,
30        message: impl Into<Cow<'static, str>>,
31        correlation_id: impl Into<Cow<'static, str>>,
32        retryability: Retryability,
33    ) -> Self {
34        Self(Box::new(AwsErrorInner {
35            kind,
36            message: message.into(),
37            correlation_id: correlation_id.into(),
38            service_error: None,
39            retryability,
40            details: None,
41        }))
42    }
43
44    /// Creates a non-retryable validation error.
45    pub fn validation(
46        message: impl Into<Cow<'static, str>>,
47        correlation_id: impl Into<Cow<'static, str>>,
48    ) -> Self {
49        Self::new(
50            AwsErrorKind::Validation,
51            message,
52            correlation_id,
53            Retryability::NonRetryable,
54        )
55    }
56
57    /// Creates a non-retryable credentials resolution error.
58    pub fn credentials(
59        message: impl Into<Cow<'static, str>>,
60        correlation_id: impl Into<Cow<'static, str>>,
61    ) -> Self {
62        Self::new(
63            AwsErrorKind::Credentials,
64            message,
65            correlation_id,
66            Retryability::NonRetryable,
67        )
68    }
69
70    /// Creates a non-retryable SigV4 signing error.
71    pub fn signing(
72        message: impl Into<Cow<'static, str>>,
73        correlation_id: impl Into<Cow<'static, str>>,
74    ) -> Self {
75        Self::new(
76            AwsErrorKind::Signing,
77            message,
78            correlation_id,
79            Retryability::NonRetryable,
80        )
81    }
82
83    /// Creates a retryable transport error.
84    pub fn transport(
85        message: impl Into<Cow<'static, str>>,
86        correlation_id: impl Into<Cow<'static, str>>,
87    ) -> Self {
88        Self::new(
89            AwsErrorKind::Transport,
90            message,
91            correlation_id,
92            Retryability::Retryable { retry_after: None },
93        )
94    }
95
96    /// Creates a retryable timeout error.
97    pub fn timeout(
98        message: impl Into<Cow<'static, str>>,
99        correlation_id: impl Into<Cow<'static, str>>,
100    ) -> Self {
101        Self::new(
102            AwsErrorKind::Timeout,
103            message,
104            correlation_id,
105            Retryability::Retryable { retry_after: None },
106        )
107    }
108
109    /// Creates a non-retryable request serialization error.
110    pub fn serialization(
111        message: impl Into<Cow<'static, str>>,
112        correlation_id: impl Into<Cow<'static, str>>,
113    ) -> Self {
114        Self::new(
115            AwsErrorKind::Serialization,
116            message,
117            correlation_id,
118            Retryability::NonRetryable,
119        )
120    }
121
122    /// Creates a non-retryable response deserialization error.
123    pub fn deserialization(
124        message: impl Into<Cow<'static, str>>,
125        correlation_id: impl Into<Cow<'static, str>>,
126    ) -> Self {
127        Self::new(
128            AwsErrorKind::Deserialization,
129            message,
130            correlation_id,
131            Retryability::NonRetryable,
132        )
133    }
134
135    /// Creates a structured AWS service error.
136    pub fn service(
137        kind: AwsErrorKind,
138        details: ServiceErrorDetails,
139        correlation_id: impl Into<Cow<'static, str>>,
140        retryability: Retryability,
141    ) -> Self {
142        let message = format!(
143            "{}::{} failed with {} (HTTP {}): {}",
144            details.service(),
145            details.operation(),
146            details.code(),
147            details.http_status(),
148            details.message()
149        );
150        Self(Box::new(AwsErrorInner {
151            kind,
152            message: Cow::Owned(message),
153            correlation_id: correlation_id.into(),
154            service_error: Some(details),
155            retryability,
156            details: None,
157        }))
158    }
159
160    /// Creates a non-retryable resource-not-found error.
161    pub fn not_found(
162        message: impl Into<Cow<'static, str>>,
163        correlation_id: impl Into<Cow<'static, str>>,
164    ) -> Self {
165        Self::new(
166            AwsErrorKind::NotFound,
167            message,
168            correlation_id,
169            Retryability::NonRetryable,
170        )
171    }
172
173    /// Creates a non-retryable already-exists error.
174    pub fn already_exists(
175        message: impl Into<Cow<'static, str>>,
176        correlation_id: impl Into<Cow<'static, str>>,
177    ) -> Self {
178        Self::new(
179            AwsErrorKind::AlreadyExists,
180            message,
181            correlation_id,
182            Retryability::NonRetryable,
183        )
184    }
185
186    /// Creates a retryable throttling error with optional `Retry-After` hint.
187    pub fn throttled(
188        message: impl Into<Cow<'static, str>>,
189        correlation_id: impl Into<Cow<'static, str>>,
190        retry_after: Option<Duration>,
191    ) -> Self {
192        Self::new(
193            AwsErrorKind::Throttled,
194            message,
195            correlation_id,
196            Retryability::Retryable { retry_after },
197        )
198    }
199
200    /// Creates a waiter timeout error.
201    pub fn waiter_timeout(
202        message: impl Into<Cow<'static, str>>,
203        correlation_id: impl Into<Cow<'static, str>>,
204    ) -> Self {
205        Self::new(
206            AwsErrorKind::WaiterTimeout,
207            message,
208            correlation_id,
209            Retryability::NonRetryable,
210        )
211    }
212
213    /// Creates a waiter cancellation error.
214    pub fn waiter_cancelled(
215        message: impl Into<Cow<'static, str>>,
216        correlation_id: impl Into<Cow<'static, str>>,
217    ) -> Self {
218        Self::new(
219            AwsErrorKind::WaiterCancelled,
220            message,
221            correlation_id,
222            Retryability::NonRetryable,
223        )
224    }
225
226    /// Creates a batch partial-failure error.
227    pub fn batch_partial_failure(
228        message: impl Into<Cow<'static, str>>,
229        correlation_id: impl Into<Cow<'static, str>>,
230    ) -> Self {
231        Self::new(
232            AwsErrorKind::BatchPartialFailure,
233            message,
234            correlation_id,
235            Retryability::NonRetryable,
236        )
237    }
238
239    /// Attaches internal diagnostic details, subject to boundary redaction.
240    pub fn with_details(mut self, details: impl Into<Cow<'static, str>>) -> Self {
241        self.0.details = Some(details.into());
242        self
243    }
244
245    /// Attaches structured AWS service error details.
246    pub fn with_service_error(mut self, service_error: ServiceErrorDetails) -> Self {
247        self.0.service_error = Some(service_error);
248        self
249    }
250
251    /// Overrides the retryability classification of this error.
252    pub fn with_retryability(mut self, retryability: Retryability) -> Self {
253        self.0.retryability = retryability;
254        self
255    }
256
257    /// Strips internal `details` unless `expose_details` is explicitly `true`.
258    pub fn redact_for_boundary(mut self, expose_details: bool) -> Self {
259        if !expose_details {
260            self.0.details = None;
261        }
262        self
263    }
264}