cortexa-aws-errors 0.1.0

Compact, redacted AWS SDK error contracts and retryability types for cortexa-rust-aws.
Documentation
//! Error categories, retryability classification, and structured AWS service error details.

use std::borrow::Cow;
use std::time::Duration;

/// High-level classification of an AWS SDK failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AwsErrorKind {
    /// Input validation or invariant check failed before sending a request.
    Validation,
    /// Credentials could not be resolved or were invalid.
    Credentials,
    /// Request signing (SigV4) failed.
    Signing,
    /// Underlying HTTP or network transport failure.
    Transport,
    /// Request or operation exceeded its configured timeout.
    Timeout,
    /// Request payload could not be serialized.
    Serialization,
    /// Response payload could not be deserialized or exceeded safety bounds.
    Deserialization,
    /// AWS service returned a structured error response.
    Service,
    /// AWS service throttled the request (e.g. HTTP 429 or `Throttling`).
    Throttled,
    /// Requested AWS resource or item was not found.
    NotFound,
    /// Resource creation failed because the resource already exists.
    AlreadyExists,
    /// A waiter exceeded its maximum attempts or timeout budget.
    WaiterTimeout,
    /// A waiter was cancelled before completion.
    WaiterCancelled,
    /// A batch operation completed with one or more failed items.
    BatchPartialFailure,
}

/// Indicates whether a failed operation can safely be retried.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Retryability {
    /// The operation may succeed on retry, optionally honoring a server-provided delay.
    Retryable {
        /// Optional server-requested delay (such as HTTP `Retry-After`).
        retry_after: Option<Duration>,
    },
    /// The error is terminal for the current inputs or state.
    NonRetryable,
}

impl Retryability {
    /// Returns `true` when the failure is retryable.
    #[must_use]
    pub const fn is_retryable(self) -> bool {
        matches!(self, Self::Retryable { .. })
    }

    /// Returns any explicit retry delay hint, if present.
    #[must_use]
    pub const fn retry_after(self) -> Option<Duration> {
        match self {
            Self::Retryable { retry_after } => retry_after,
            Self::NonRetryable => None,
        }
    }
}

/// Metadata returned by an AWS service in an error response.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServiceErrorDetails {
    service: Cow<'static, str>,
    operation: Cow<'static, str>,
    code: String,
    message: String,
    http_status: u16,
    request_id: Option<String>,
}

impl ServiceErrorDetails {
    /// Constructs structured AWS service error details.
    #[must_use]
    pub fn new(
        service: impl Into<Cow<'static, str>>,
        operation: impl Into<Cow<'static, str>>,
        code: impl Into<String>,
        message: impl Into<String>,
        http_status: u16,
        request_id: Option<String>,
    ) -> Self {
        Self {
            service: service.into(),
            operation: operation.into(),
            code: code.into(),
            message: message.into(),
            http_status,
            request_id,
        }
    }

    /// Returns the AWS service identifier (e.g. `"s3"`, `"dynamodb"`, `"ec2"`).
    #[must_use]
    pub fn service(&self) -> &str {
        &self.service
    }

    /// Returns the AWS API operation name (e.g. `"HeadBucket"`).
    #[must_use]
    pub fn operation(&self) -> &str {
        &self.operation
    }

    /// Returns the AWS error code returned by the service.
    #[must_use]
    pub fn code(&self) -> &str {
        &self.code
    }

    /// Returns the human-readable error message from the service.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Returns the HTTP status code associated with the service error.
    #[must_use]
    pub const fn http_status(&self) -> u16 {
        self.http_status
    }

    /// Returns the AWS request identifier when provided in headers or body.
    #[must_use]
    pub fn request_id(&self) -> Option<&str> {
        self.request_id.as_deref()
    }
}