cortexa-aws-errors 0.1.0

Compact, redacted AWS SDK error contracts and retryability types for cortexa-rust-aws.
Documentation
//! Ergonomic predicates, accessors, and `Display` / `Error` implementations for [`AwsError`].

use std::fmt;

use crate::error::AwsError;
use crate::kind::{AwsErrorKind, Retryability, ServiceErrorDetails};

impl AwsError {
    /// Returns the error classification.
    #[must_use]
    #[inline]
    pub fn kind(&self) -> AwsErrorKind {
        self.0.kind
    }

    /// Returns `true` when the target resource or key was not found.
    #[must_use]
    #[inline]
    pub fn is_not_found(&self) -> bool {
        self.0.kind == AwsErrorKind::NotFound
    }

    /// Returns `true` when resource creation failed because it already exists.
    #[must_use]
    #[inline]
    pub fn is_already_exists(&self) -> bool {
        self.0.kind == AwsErrorKind::AlreadyExists
    }

    /// Returns `true` when the request was throttled by the AWS service or retry budget.
    #[must_use]
    #[inline]
    pub fn is_throttled(&self) -> bool {
        self.0.kind == AwsErrorKind::Throttled
    }

    /// Returns `true` when either an HTTP call or a waiter timed out.
    #[must_use]
    #[inline]
    pub fn is_timeout(&self) -> bool {
        matches!(
            self.0.kind,
            AwsErrorKind::Timeout | AwsErrorKind::WaiterTimeout
        )
    }

    /// Returns `true` when credential resolution or authorization failed.
    #[must_use]
    #[inline]
    pub fn is_credentials(&self) -> bool {
        self.0.kind == AwsErrorKind::Credentials
    }

    /// Returns `true` when client-side validation failed before sending a request.
    #[must_use]
    #[inline]
    pub fn is_validation(&self) -> bool {
        self.0.kind == AwsErrorKind::Validation
    }

    /// Returns the HTTP status code reported by the AWS service, if available.
    #[must_use]
    #[inline]
    pub fn http_status(&self) -> Option<u16> {
        self.0
            .service_error
            .as_ref()
            .map(ServiceErrorDetails::http_status)
    }

    /// Returns the structured AWS service error code (e.g. `"NoSuchBucket"`), if available.
    #[must_use]
    #[inline]
    pub fn error_code(&self) -> Option<&str> {
        self.0.service_error.as_ref().map(ServiceErrorDetails::code)
    }

    /// Returns the AWS request identifier (`x-amz-request-id` / `x-amzn-requestid`), if available.
    #[must_use]
    #[inline]
    pub fn request_id(&self) -> Option<&str> {
        self.0
            .service_error
            .as_ref()
            .and_then(ServiceErrorDetails::request_id)
    }

    /// Returns the primary error message.
    #[must_use]
    #[inline]
    pub fn message(&self) -> &str {
        &self.0.message
    }

    /// Returns the correlation identifier for tracing across retries and waiters.
    #[must_use]
    #[inline]
    pub fn correlation_id(&self) -> &str {
        &self.0.correlation_id
    }

    /// Returns structured AWS service error details, if any.
    #[must_use]
    #[inline]
    pub fn service_error(&self) -> Option<&ServiceErrorDetails> {
        self.0.service_error.as_ref()
    }

    /// Returns retryability metadata.
    #[must_use]
    #[inline]
    pub fn retryability(&self) -> Retryability {
        self.0.retryability
    }

    /// Returns `true` if the error is retryable.
    #[must_use]
    #[inline]
    pub fn is_retryable(&self) -> bool {
        self.0.retryability.is_retryable()
    }

    /// Returns optional diagnostic details when exposure was enabled.
    #[must_use]
    #[inline]
    pub fn details(&self) -> Option<&str> {
        self.0.details.as_deref()
    }
}

impl fmt::Display for AwsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{:?}:{}] {}",
            self.0.kind, self.0.correlation_id, self.0.message
        )?;
        if let Some(details) = &self.0.details {
            write!(f, " ({details})")?;
        }
        Ok(())
    }
}

impl std::error::Error for AwsError {}