Skip to main content

rustack_cloudwatch_model/
error.rs

1//! Auto-generated from AWS CloudWatch Smithy model. DO NOT EDIT.
2//!
3//! CloudWatch errors use JSON format with a `__type` field containing the
4//! short error type name (e.g., `ResourceNotFoundException`).
5
6use std::fmt;
7
8/// Well-known CloudWatch error codes.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum CloudWatchErrorCode {
12    /// ConcurrentModificationException error.
13    #[default]
14    ConcurrentModificationException,
15    /// ConflictException error.
16    ConflictException,
17    /// DashboardInvalidInputError error.
18    DashboardInvalidInputError,
19    /// DashboardNotFoundError error.
20    DashboardNotFoundError,
21    /// InternalServiceFault error.
22    InternalServiceFault,
23    /// InvalidAction error.
24    InvalidAction,
25    /// InvalidFormatFault error.
26    InvalidFormatFault,
27    /// InvalidNextToken error.
28    InvalidNextToken,
29    /// InvalidParameterCombinationException error.
30    InvalidParameterCombinationException,
31    /// InvalidParameterValueException error.
32    InvalidParameterValueException,
33    /// LimitExceededException error.
34    LimitExceededException,
35    /// LimitExceededFault error.
36    LimitExceededFault,
37    /// MissingAction error.
38    MissingAction,
39    /// MissingRequiredParameterException error.
40    MissingRequiredParameterException,
41    /// ResourceNotFound error.
42    ResourceNotFound,
43    /// ResourceNotFoundException error.
44    ResourceNotFoundException,
45}
46
47impl CloudWatchErrorCode {
48    /// Returns the short error type string for the JSON `__type` field.
49    #[must_use]
50    pub fn error_type(&self) -> &'static str {
51        self.as_str()
52    }
53
54    /// Returns the short error code string.
55    #[must_use]
56    pub fn as_str(&self) -> &'static str {
57        match self {
58            Self::ConcurrentModificationException => "ConcurrentModificationException",
59            Self::ConflictException => "ConflictException",
60            Self::DashboardInvalidInputError => "DashboardInvalidInputError",
61            Self::DashboardNotFoundError => "DashboardNotFoundError",
62            Self::InternalServiceFault => "InternalServiceFault",
63            Self::InvalidAction => "InvalidAction",
64            Self::InvalidFormatFault => "InvalidFormatFault",
65            Self::InvalidNextToken => "InvalidNextToken",
66            Self::InvalidParameterCombinationException => "InvalidParameterCombinationException",
67            Self::InvalidParameterValueException => "InvalidParameterValueException",
68            Self::LimitExceededException => "LimitExceededException",
69            Self::LimitExceededFault => "LimitExceededFault",
70            Self::MissingAction => "MissingAction",
71            Self::MissingRequiredParameterException => "MissingRequiredParameterException",
72            Self::ResourceNotFound => "ResourceNotFound",
73            Self::ResourceNotFoundException => "ResourceNotFoundException",
74        }
75    }
76
77    /// Returns the default HTTP status code for this error.
78    #[must_use]
79    pub fn default_status_code(&self) -> http::StatusCode {
80        match self {
81            Self::DashboardInvalidInputError
82            | Self::InvalidAction
83            | Self::InvalidFormatFault
84            | Self::InvalidNextToken
85            | Self::InvalidParameterCombinationException
86            | Self::InvalidParameterValueException
87            | Self::LimitExceededException
88            | Self::LimitExceededFault
89            | Self::MissingAction
90            | Self::MissingRequiredParameterException => http::StatusCode::BAD_REQUEST,
91            Self::DashboardNotFoundError
92            | Self::ResourceNotFound
93            | Self::ResourceNotFoundException => http::StatusCode::NOT_FOUND,
94            Self::ConflictException => http::StatusCode::CONFLICT,
95            Self::ConcurrentModificationException | Self::InternalServiceFault => {
96                http::StatusCode::INTERNAL_SERVER_ERROR
97            }
98        }
99    }
100}
101
102impl fmt::Display for CloudWatchErrorCode {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        f.write_str(self.as_str())
105    }
106}
107
108/// An CloudWatch error response.
109#[derive(Debug)]
110pub struct CloudWatchError {
111    /// The error code.
112    pub code: CloudWatchErrorCode,
113    /// A human-readable error message.
114    pub message: String,
115    /// The HTTP status code.
116    pub status_code: http::StatusCode,
117    /// The underlying source error, if any.
118    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
119}
120
121impl fmt::Display for CloudWatchError {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        write!(f, "CloudWatchError({}): {}", self.code, self.message)
124    }
125}
126
127impl std::error::Error for CloudWatchError {
128    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
129        self.source
130            .as_ref()
131            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
132    }
133}
134
135impl CloudWatchError {
136    /// Create a new `CloudWatchError` from an error code.
137    #[must_use]
138    pub fn new(code: CloudWatchErrorCode) -> Self {
139        Self {
140            status_code: code.default_status_code(),
141            message: code.as_str().to_owned(),
142            code,
143            source: None,
144        }
145    }
146
147    /// Create a new `CloudWatchError` with a custom message.
148    #[must_use]
149    pub fn with_message(code: CloudWatchErrorCode, message: impl Into<String>) -> Self {
150        Self {
151            status_code: code.default_status_code(),
152            message: message.into(),
153            code,
154            source: None,
155        }
156    }
157
158    /// Returns the `__type` string for the JSON error response.
159    #[must_use]
160    pub fn error_type(&self) -> &'static str {
161        self.code.error_type()
162    }
163
164    /// Internal error.
165    #[must_use]
166    pub fn internal_error(message: impl Into<String>) -> Self {
167        Self::with_message(CloudWatchErrorCode::InternalServiceFault, message)
168    }
169
170    /// Missing action header.
171    #[must_use]
172    pub fn missing_action() -> Self {
173        Self::with_message(
174            CloudWatchErrorCode::MissingAction,
175            "Missing required header: X-Amz-Target",
176        )
177    }
178
179    /// Unknown operation.
180    #[must_use]
181    pub fn unknown_operation(target: &str) -> Self {
182        Self::with_message(
183            CloudWatchErrorCode::InvalidAction,
184            format!("Operation {target} is not supported."),
185        )
186    }
187
188    /// Not implemented.
189    #[must_use]
190    pub fn not_implemented(operation: &str) -> Self {
191        Self::with_message(
192            CloudWatchErrorCode::InternalServiceFault,
193            format!("Operation {operation} is not yet implemented"),
194        )
195    }
196}
197
198/// Create an `CloudWatchError` from an error code.
199///
200/// # Examples
201///
202/// ```ignore
203/// let err = cloudwatch_error!(ConcurrentModificationException);
204/// assert_eq!(err.code, CloudWatchErrorCode::ConcurrentModificationException);
205/// ```
206#[macro_export]
207macro_rules! cloudwatch_error {
208    ($code:ident) => {
209        $crate::error::CloudWatchError::new($crate::error::CloudWatchErrorCode::$code)
210    };
211    ($code:ident, $msg:expr) => {
212        $crate::error::CloudWatchError::with_message(
213            $crate::error::CloudWatchErrorCode::$code,
214            $msg,
215        )
216    };
217}