use std::borrow::Cow;
use std::time::Duration;
use crate::kind::{AwsErrorKind, Retryability, ServiceErrorDetails};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AwsErrorInner {
pub(crate) kind: AwsErrorKind,
pub(crate) message: Cow<'static, str>,
pub(crate) correlation_id: Cow<'static, str>,
pub(crate) service_error: Option<ServiceErrorDetails>,
pub(crate) retryability: Retryability,
pub(crate) details: Option<Cow<'static, str>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct AwsError(pub(crate) Box<AwsErrorInner>);
impl AwsError {
pub fn new(
kind: AwsErrorKind,
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
retryability: Retryability,
) -> Self {
Self(Box::new(AwsErrorInner {
kind,
message: message.into(),
correlation_id: correlation_id.into(),
service_error: None,
retryability,
details: None,
}))
}
pub fn validation(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Validation,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn credentials(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Credentials,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn signing(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Signing,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn transport(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Transport,
message,
correlation_id,
Retryability::Retryable { retry_after: None },
)
}
pub fn timeout(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Timeout,
message,
correlation_id,
Retryability::Retryable { retry_after: None },
)
}
pub fn serialization(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Serialization,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn deserialization(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::Deserialization,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn service(
kind: AwsErrorKind,
details: ServiceErrorDetails,
correlation_id: impl Into<Cow<'static, str>>,
retryability: Retryability,
) -> Self {
let message = format!(
"{}::{} failed with {} (HTTP {}): {}",
details.service(),
details.operation(),
details.code(),
details.http_status(),
details.message()
);
Self(Box::new(AwsErrorInner {
kind,
message: Cow::Owned(message),
correlation_id: correlation_id.into(),
service_error: Some(details),
retryability,
details: None,
}))
}
pub fn not_found(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::NotFound,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn already_exists(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::AlreadyExists,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn throttled(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
retry_after: Option<Duration>,
) -> Self {
Self::new(
AwsErrorKind::Throttled,
message,
correlation_id,
Retryability::Retryable { retry_after },
)
}
pub fn waiter_timeout(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::WaiterTimeout,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn waiter_cancelled(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::WaiterCancelled,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn batch_partial_failure(
message: impl Into<Cow<'static, str>>,
correlation_id: impl Into<Cow<'static, str>>,
) -> Self {
Self::new(
AwsErrorKind::BatchPartialFailure,
message,
correlation_id,
Retryability::NonRetryable,
)
}
pub fn with_details(mut self, details: impl Into<Cow<'static, str>>) -> Self {
self.0.details = Some(details.into());
self
}
pub fn with_service_error(mut self, service_error: ServiceErrorDetails) -> Self {
self.0.service_error = Some(service_error);
self
}
pub fn with_retryability(mut self, retryability: Retryability) -> Self {
self.0.retryability = retryability;
self
}
pub fn redact_for_boundary(mut self, expose_details: bool) -> Self {
if !expose_details {
self.0.details = None;
}
self
}
}