rustack_logs_model/
error.rs1use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10#[non_exhaustive]
11pub enum LogsErrorCode {
12 DataAlreadyAcceptedException,
14 InvalidAction,
16 InvalidOperationException,
18 InvalidParameterException,
20 InvalidSequenceTokenException,
22 LimitExceededException,
24 MalformedQueryException,
26 MissingAction,
28 OperationAbortedException,
30 ResourceAlreadyExistsException,
32 ResourceNotFoundException,
34 ServiceUnavailableException,
36 TooManyTagsException,
38 UnrecognizedClientException,
40 #[default]
42 ValidationException,
43}
44
45impl LogsErrorCode {
46 #[must_use]
48 pub fn error_type(&self) -> &'static str {
49 self.as_str()
50 }
51
52 #[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 #[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#[derive(Debug)]
105pub struct LogsError {
106 pub code: LogsErrorCode,
108 pub message: String,
110 pub status_code: http::StatusCode,
112 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 #[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 #[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 #[must_use]
155 pub fn error_type(&self) -> &'static str {
156 self.code.error_type()
157 }
158
159 #[must_use]
161 pub fn validation(message: impl Into<String>) -> Self {
162 Self::with_message(LogsErrorCode::ValidationException, message)
163 }
164
165 #[must_use]
167 pub fn internal_error(message: impl Into<String>) -> Self {
168 Self::with_message(LogsErrorCode::ServiceUnavailableException, message)
169 }
170
171 #[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 #[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 #[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#[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}