cortexa-aws-errors 0.1.0

Compact, redacted AWS SDK error contracts and retryability types for cortexa-rust-aws.
Documentation
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

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

use super::*;

#[test]
fn aws_error_is_pointer_sized_with_niche() {
    assert_eq!(
        std::mem::size_of::<AwsError>(),
        std::mem::size_of::<usize>()
    );
    assert_eq!(
        std::mem::size_of::<Option<AwsError>>(),
        std::mem::size_of::<usize>()
    );
}

#[test]
fn redaction_strips_details_by_default() {
    let err = AwsError::transport("connection reset", "cid-1")
        .with_details("tcp socket 10.0.0.1:443 closed");
    assert_eq!(err.details(), Some("tcp socket 10.0.0.1:443 closed"));

    let redacted = err.clone().redact_for_boundary(false);
    assert_eq!(redacted.details(), None);
    assert!(!redacted.to_string().contains("10.0.0.1"));

    let exposed = err.redact_for_boundary(true);
    assert_eq!(exposed.details(), Some("tcp socket 10.0.0.1:443 closed"));
}

#[test]
fn retryability_and_service_details_work() {
    let details = ServiceErrorDetails::new(
        "dynamodb",
        "BatchWriteItem",
        "ProvisionedThroughputExceededException",
        "Rate of requests exceeds the allowed throughput",
        400,
        Some("req-123".to_owned()),
    );
    let err = AwsError::service(
        AwsErrorKind::Throttled,
        details,
        "cid-2",
        Retryability::Retryable {
            retry_after: Some(Duration::from_secs(2)),
        },
    );

    assert!(err.is_retryable());
    assert!(err.is_throttled());
    assert!(!err.is_not_found());
    assert_eq!(err.http_status(), Some(400));
    assert_eq!(
        err.error_code(),
        Some("ProvisionedThroughputExceededException")
    );
    assert_eq!(err.request_id(), Some("req-123"));
    assert_eq!(
        err.retryability().retry_after(),
        Some(Duration::from_secs(2))
    );
    let svc = err.service_error().unwrap();
    assert_eq!(svc.service(), "dynamodb");
    assert_eq!(svc.operation(), "BatchWriteItem");
    assert_eq!(svc.http_status(), 400);
    assert_eq!(svc.request_id(), Some("req-123"));
}

#[test]
fn predicates_and_static_cow_allocations_work() {
    let nf = AwsError::not_found("missing", "cid-nf");
    assert!(nf.is_not_found());
    assert!(matches!(nf.0.message, Cow::Borrowed("missing")));
    assert!(matches!(nf.0.correlation_id, Cow::Borrowed("cid-nf")));

    assert!(AwsError::already_exists("dup", "c").is_already_exists());
    assert!(AwsError::timeout("t", "c").is_timeout());
    assert!(AwsError::waiter_timeout("wt", "c").is_timeout());
    assert!(AwsError::credentials("cred", "c").is_credentials());
    assert!(AwsError::validation("val", "c").is_validation());
    assert_eq!(nf.http_status(), None);
    assert_eq!(nf.error_code(), None);
    assert_eq!(nf.request_id(), None);
}

#[test]
fn all_constructors_mutators_and_error_trait_work() {
    use std::error::Error as _;

    let th = AwsError::throttled("slow down", "cid-th", Some(Duration::from_millis(250)));
    assert!(th.is_throttled());
    assert!(th.is_retryable());
    assert_eq!(
        th.retryability().retry_after(),
        Some(Duration::from_millis(250))
    );
    assert_eq!(th.correlation_id(), "cid-th");
    assert_eq!(th.message(), "slow down");

    let wc = AwsError::waiter_cancelled("cancelled", "cid-wc");
    assert_eq!(wc.kind(), AwsErrorKind::WaiterCancelled);
    assert!(!wc.is_retryable());

    let bp = AwsError::batch_partial_failure("2 failed", "cid-bp");
    assert_eq!(bp.kind(), AwsErrorKind::BatchPartialFailure);

    let ser = AwsError::serialization("bad json", "cid-ser");
    assert_eq!(ser.kind(), AwsErrorKind::Serialization);

    let de = AwsError::deserialization("bad xml", "cid-de");
    assert_eq!(de.kind(), AwsErrorKind::Deserialization);

    let sig = AwsError::signing("bad key", "cid-sig");
    assert_eq!(sig.kind(), AwsErrorKind::Signing);

    let details = ServiceErrorDetails::new(
        "s3",
        "PutObject",
        "SlowDown",
        "Reduce request rate",
        503,
        None,
    );
    let mutated = ser
        .with_service_error(details)
        .with_retryability(Retryability::Retryable { retry_after: None })
        .with_details("internal trace");
    assert!(mutated.is_retryable());
    assert_eq!(mutated.http_status(), Some(503));
    assert_eq!(mutated.error_code(), Some("SlowDown"));
    assert_eq!(mutated.request_id(), None);
    assert!(mutated.source().is_none());
    let rendered = mutated.to_string();
    assert!(rendered.contains("cid-ser"));
}