Unverified Commit a9b6e6e1 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Deserialize Extended S3 Errors (#429)

* Add customization for S3 host ID

* Cleanup and fully replace the s3 protocol

* Only generate the customization for S3

* Fix bugs which caused rustfmt to crash

* Add test

* Back out unused change

* Update aws/sdk/integration-tests/s3/Cargo.toml

* CR feedback, add missing test
parent 362e3440
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
[package]
name = "inlineable-aws"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"
description = """
The modules of this crate are intended to be inlined directly into the SDK as needed. The dependencies here
are to allow this crate to be compilable and testable in isolation, no client code actually takes these dependencies.
"""

[dependencies]
smithy-xml = { path = "../../../rust-runtime/smithy-xml" }
smithy-types = { path = "../../../rust-runtime/smithy-types" }
http = "0.2.4"
+7 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#[allow(dead_code)]
mod s3_errors;
+72 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

const EXTENDED_REQUEST_ID: &str = "s3_extended_request_id";

pub trait ErrorExt {
    fn extended_request_id(&self) -> Option<&str>;
}

impl ErrorExt for smithy_types::Error {
    fn extended_request_id(&self) -> Option<&str> {
        self.extra(EXTENDED_REQUEST_ID)
    }
}

pub fn parse_extended_error<B>(
    error: smithy_types::Error,
    response: &http::Response<B>,
) -> smithy_types::Error {
    let mut builder = error.into_builder();
    let host_id = response
        .headers()
        .get("x-amz-id-2")
        .and_then(|header_value| header_value.to_str().ok());
    if let Some(host_id) = host_id {
        builder.custom(EXTENDED_REQUEST_ID, host_id);
    }
    builder.build()
}

#[cfg(test)]
mod test {
    use crate::s3_errors::{parse_extended_error, ErrorExt};

    #[test]
    fn add_error_fields() {
        let resp = http::Response::builder()
            .header(
                "x-amz-id-2",
                "eftixk72aD6Ap51TnqcoF8eFidJG9Z/2mkiDFu8yU9AS1ed4OpIszj7UDNEHGran",
            )
            .status(400)
            .body("")
            .unwrap();
        let error = smithy_types::Error::builder()
            .message("123")
            .request_id("456")
            .build();

        let error = parse_extended_error(error, &resp);
        assert_eq!(
            error
                .extended_request_id()
                .expect("extended request id should be set"),
            "eftixk72aD6Ap51TnqcoF8eFidJG9Z/2mkiDFu8yU9AS1ed4OpIszj7UDNEHGran"
        );
    }

    #[test]
    fn handle_missing_header() {
        let resp = http::Response::builder().status(400).body("").unwrap();
        let error = smithy_types::Error::builder()
            .message("123")
            .request_id("456")
            .build();

        let error = parse_extended_error(error, &resp);
        assert_eq!(error.extended_request_id(), None);
    }
}
+20 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

description = "Rust Runtime"
plugins {
    kotlin("jvm")
}

group = "software.amazon.aws.rustruntime"

version = "0.0.3"

tasks.jar {
    from("./") {
        include("aws-inlineable/src/*.rs")
        include("aws-inlineable/Cargo.toml")
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ val kotestVersion: String by project

dependencies {
    implementation(project(":codegen"))
    runtimeOnly(project(":aws:rust-runtime"))
    implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
Loading