Skip to main content

cortexa_aws_errors/
predicates.rs

1//! Ergonomic predicates, accessors, and `Display` / `Error` implementations for [`AwsError`].
2
3use std::fmt;
4
5use crate::error::AwsError;
6use crate::kind::{AwsErrorKind, Retryability, ServiceErrorDetails};
7
8impl AwsError {
9    /// Returns the error classification.
10    #[must_use]
11    #[inline]
12    pub fn kind(&self) -> AwsErrorKind {
13        self.0.kind
14    }
15
16    /// Returns `true` when the target resource or key was not found.
17    #[must_use]
18    #[inline]
19    pub fn is_not_found(&self) -> bool {
20        self.0.kind == AwsErrorKind::NotFound
21    }
22
23    /// Returns `true` when resource creation failed because it already exists.
24    #[must_use]
25    #[inline]
26    pub fn is_already_exists(&self) -> bool {
27        self.0.kind == AwsErrorKind::AlreadyExists
28    }
29
30    /// Returns `true` when the request was throttled by the AWS service or retry budget.
31    #[must_use]
32    #[inline]
33    pub fn is_throttled(&self) -> bool {
34        self.0.kind == AwsErrorKind::Throttled
35    }
36
37    /// Returns `true` when either an HTTP call or a waiter timed out.
38    #[must_use]
39    #[inline]
40    pub fn is_timeout(&self) -> bool {
41        matches!(
42            self.0.kind,
43            AwsErrorKind::Timeout | AwsErrorKind::WaiterTimeout
44        )
45    }
46
47    /// Returns `true` when credential resolution or authorization failed.
48    #[must_use]
49    #[inline]
50    pub fn is_credentials(&self) -> bool {
51        self.0.kind == AwsErrorKind::Credentials
52    }
53
54    /// Returns `true` when client-side validation failed before sending a request.
55    #[must_use]
56    #[inline]
57    pub fn is_validation(&self) -> bool {
58        self.0.kind == AwsErrorKind::Validation
59    }
60
61    /// Returns the HTTP status code reported by the AWS service, if available.
62    #[must_use]
63    #[inline]
64    pub fn http_status(&self) -> Option<u16> {
65        self.0
66            .service_error
67            .as_ref()
68            .map(ServiceErrorDetails::http_status)
69    }
70
71    /// Returns the structured AWS service error code (e.g. `"NoSuchBucket"`), if available.
72    #[must_use]
73    #[inline]
74    pub fn error_code(&self) -> Option<&str> {
75        self.0.service_error.as_ref().map(ServiceErrorDetails::code)
76    }
77
78    /// Returns the AWS request identifier (`x-amz-request-id` / `x-amzn-requestid`), if available.
79    #[must_use]
80    #[inline]
81    pub fn request_id(&self) -> Option<&str> {
82        self.0
83            .service_error
84            .as_ref()
85            .and_then(ServiceErrorDetails::request_id)
86    }
87
88    /// Returns the primary error message.
89    #[must_use]
90    #[inline]
91    pub fn message(&self) -> &str {
92        &self.0.message
93    }
94
95    /// Returns the correlation identifier for tracing across retries and waiters.
96    #[must_use]
97    #[inline]
98    pub fn correlation_id(&self) -> &str {
99        &self.0.correlation_id
100    }
101
102    /// Returns structured AWS service error details, if any.
103    #[must_use]
104    #[inline]
105    pub fn service_error(&self) -> Option<&ServiceErrorDetails> {
106        self.0.service_error.as_ref()
107    }
108
109    /// Returns retryability metadata.
110    #[must_use]
111    #[inline]
112    pub fn retryability(&self) -> Retryability {
113        self.0.retryability
114    }
115
116    /// Returns `true` if the error is retryable.
117    #[must_use]
118    #[inline]
119    pub fn is_retryable(&self) -> bool {
120        self.0.retryability.is_retryable()
121    }
122
123    /// Returns optional diagnostic details when exposure was enabled.
124    #[must_use]
125    #[inline]
126    pub fn details(&self) -> Option<&str> {
127        self.0.details.as_deref()
128    }
129}
130
131impl fmt::Display for AwsError {
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        write!(
134            f,
135            "[{:?}:{}] {}",
136            self.0.kind, self.0.correlation_id, self.0.message
137        )?;
138        if let Some(details) = &self.0.details {
139            write!(f, " ({details})")?;
140        }
141        Ok(())
142    }
143}
144
145impl std::error::Error for AwsError {}