cortexa-aws-errors 0.1.0

Compact, redacted AWS SDK error contracts and retryability types for cortexa-rust-aws.
Documentation
//! Pointer-sized [`AwsError`] type, constructors, and boundary redaction mutators.

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>>,
}

/// Compact pointer-sized error type (`size_of::<AwsError>() == size_of::<usize>()`).
///
/// Boxing the inner payload keeps `Result<T, AwsError>` and `async` future state
/// machines minimal on the stack on the happy path.
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct AwsError(pub(crate) Box<AwsErrorInner>);

impl AwsError {
    /// Creates an `AwsError` with full control over classification, retryability, and details.
    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,
        }))
    }

    /// Creates a non-retryable validation error.
    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,
        )
    }

    /// Creates a non-retryable credentials resolution error.
    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,
        )
    }

    /// Creates a non-retryable SigV4 signing error.
    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,
        )
    }

    /// Creates a retryable transport error.
    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 },
        )
    }

    /// Creates a retryable timeout error.
    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 },
        )
    }

    /// Creates a non-retryable request serialization error.
    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,
        )
    }

    /// Creates a non-retryable response deserialization error.
    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,
        )
    }

    /// Creates a structured AWS service error.
    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,
        }))
    }

    /// Creates a non-retryable resource-not-found error.
    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,
        )
    }

    /// Creates a non-retryable already-exists error.
    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,
        )
    }

    /// Creates a retryable throttling error with optional `Retry-After` hint.
    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 },
        )
    }

    /// Creates a waiter timeout error.
    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,
        )
    }

    /// Creates a waiter cancellation error.
    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,
        )
    }

    /// Creates a batch partial-failure error.
    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,
        )
    }

    /// Attaches internal diagnostic details, subject to boundary redaction.
    pub fn with_details(mut self, details: impl Into<Cow<'static, str>>) -> Self {
        self.0.details = Some(details.into());
        self
    }

    /// Attaches structured AWS service error details.
    pub fn with_service_error(mut self, service_error: ServiceErrorDetails) -> Self {
        self.0.service_error = Some(service_error);
        self
    }

    /// Overrides the retryability classification of this error.
    pub fn with_retryability(mut self, retryability: Retryability) -> Self {
        self.0.retryability = retryability;
        self
    }

    /// Strips internal `details` unless `expose_details` is explicitly `true`.
    pub fn redact_for_boundary(mut self, expose_details: bool) -> Self {
        if !expose_details {
            self.0.details = None;
        }
        self
    }
}