Skip to main content

rustack_logs_model/
error.rs

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