rustack_cloudwatch_model/
error.rs1use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum CloudWatchErrorCode {
12 #[default]
14 ConcurrentModificationException,
15 ConflictException,
17 DashboardInvalidInputError,
19 DashboardNotFoundError,
21 InternalServiceFault,
23 InvalidAction,
25 InvalidFormatFault,
27 InvalidNextToken,
29 InvalidParameterCombinationException,
31 InvalidParameterValueException,
33 LimitExceededException,
35 LimitExceededFault,
37 MissingAction,
39 MissingRequiredParameterException,
41 ResourceNotFound,
43 ResourceNotFoundException,
45}
46
47impl CloudWatchErrorCode {
48 #[must_use]
50 pub fn error_type(&self) -> &'static str {
51 self.as_str()
52 }
53
54 #[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 #[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#[derive(Debug)]
110pub struct CloudWatchError {
111 pub code: CloudWatchErrorCode,
113 pub message: String,
115 pub status_code: http::StatusCode,
117 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 #[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 #[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 #[must_use]
160 pub fn error_type(&self) -> &'static str {
161 self.code.error_type()
162 }
163
164 #[must_use]
166 pub fn internal_error(message: impl Into<String>) -> Self {
167 Self::with_message(CloudWatchErrorCode::InternalServiceFault, message)
168 }
169
170 #[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 #[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 #[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#[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}