use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum LogsErrorCode {
DataAlreadyAcceptedException,
InvalidAction,
InvalidOperationException,
InvalidParameterException,
InvalidSequenceTokenException,
LimitExceededException,
MalformedQueryException,
MissingAction,
OperationAbortedException,
ResourceAlreadyExistsException,
ResourceNotFoundException,
ServiceUnavailableException,
TooManyTagsException,
UnrecognizedClientException,
#[default]
ValidationException,
}
impl LogsErrorCode {
#[must_use]
pub fn error_type(&self) -> &'static str {
self.as_str()
}
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::DataAlreadyAcceptedException => "DataAlreadyAcceptedException",
Self::InvalidAction => "InvalidAction",
Self::InvalidOperationException => "InvalidOperationException",
Self::InvalidParameterException => "InvalidParameterException",
Self::InvalidSequenceTokenException => "InvalidSequenceTokenException",
Self::LimitExceededException => "LimitExceededException",
Self::MalformedQueryException => "MalformedQueryException",
Self::MissingAction => "MissingAction",
Self::OperationAbortedException => "OperationAbortedException",
Self::ResourceAlreadyExistsException => "ResourceAlreadyExistsException",
Self::ResourceNotFoundException => "ResourceNotFoundException",
Self::ServiceUnavailableException => "ServiceUnavailableException",
Self::TooManyTagsException => "TooManyTagsException",
Self::UnrecognizedClientException => "UnrecognizedClientException",
Self::ValidationException => "ValidationException",
}
}
#[must_use]
pub fn default_status_code(&self) -> http::StatusCode {
match self {
Self::DataAlreadyAcceptedException
| Self::InvalidAction
| Self::InvalidOperationException
| Self::InvalidParameterException
| Self::InvalidSequenceTokenException
| Self::LimitExceededException
| Self::MalformedQueryException
| Self::MissingAction
| Self::OperationAbortedException
| Self::ResourceAlreadyExistsException
| Self::ResourceNotFoundException
| Self::TooManyTagsException
| Self::UnrecognizedClientException
| Self::ValidationException => http::StatusCode::BAD_REQUEST,
Self::ServiceUnavailableException => http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl fmt::Display for LogsErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug)]
pub struct LogsError {
pub code: LogsErrorCode,
pub message: String,
pub status_code: http::StatusCode,
pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl fmt::Display for LogsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LogsError({}): {}", self.code, self.message)
}
}
impl std::error::Error for LogsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
}
}
impl LogsError {
#[must_use]
pub fn new(code: LogsErrorCode) -> Self {
Self {
status_code: code.default_status_code(),
message: code.as_str().to_owned(),
code,
source: None,
}
}
#[must_use]
pub fn with_message(code: LogsErrorCode, message: impl Into<String>) -> Self {
Self {
status_code: code.default_status_code(),
message: message.into(),
code,
source: None,
}
}
#[must_use]
pub fn error_type(&self) -> &'static str {
self.code.error_type()
}
#[must_use]
pub fn validation(message: impl Into<String>) -> Self {
Self::with_message(LogsErrorCode::ValidationException, message)
}
#[must_use]
pub fn internal_error(message: impl Into<String>) -> Self {
Self::with_message(LogsErrorCode::ServiceUnavailableException, message)
}
#[must_use]
pub fn missing_action() -> Self {
Self::with_message(
LogsErrorCode::MissingAction,
"Missing required header: X-Amz-Target",
)
}
#[must_use]
pub fn unknown_operation(target: &str) -> Self {
Self::with_message(
LogsErrorCode::InvalidAction,
format!("Operation {target} is not supported."),
)
}
#[must_use]
pub fn not_implemented(operation: &str) -> Self {
Self::with_message(
LogsErrorCode::ServiceUnavailableException,
format!("Operation {operation} is not yet implemented"),
)
}
}
#[macro_export]
macro_rules! logs_error {
($code:ident) => {
$crate::error::LogsError::new($crate::error::LogsErrorCode::$code)
};
($code:ident, $msg:expr) => {
$crate::error::LogsError::with_message($crate::error::LogsErrorCode::$code, $msg)
};
}