-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
23 changed files
with
346 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
#[allow(dead_code)] | ||
mod s3_errors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/InlineDependency.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.rustsdk | ||
|
||
import software.amazon.smithy.rust.codegen.rustlang.InlineDependency | ||
|
||
object InlineAwsDependency { | ||
fun forRustFile(file: String): InlineDependency = InlineDependency.Companion.forRustFile(file, "aws-inlineable") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/s3/S3Decorator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.rustsdk.customize.s3 | ||
|
||
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import software.amazon.smithy.rust.codegen.rustlang.CargoDependency | ||
import software.amazon.smithy.rust.codegen.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.rustlang.asType | ||
import software.amazon.smithy.rust.codegen.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.rustlang.rustBlockTemplate | ||
import software.amazon.smithy.rust.codegen.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.smithy.RuntimeType | ||
import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization | ||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsSection | ||
import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig | ||
import software.amazon.smithy.rust.codegen.smithy.letIf | ||
import software.amazon.smithy.rust.codegen.smithy.protocols.ProtocolMap | ||
import software.amazon.smithy.rust.codegen.smithy.protocols.RestXml | ||
import software.amazon.smithy.rust.codegen.smithy.protocols.RestXmlFactory | ||
import software.amazon.smithy.rustsdk.AwsRuntimeType | ||
|
||
/** | ||
* Top level decorator for S3 | ||
* */ | ||
class S3Decorator : RustCodegenDecorator { | ||
override val name: String = "S3ExtendedError" | ||
override val order: Byte = 0 | ||
private fun applies(serviceId: ShapeId) = | ||
serviceId == ShapeId.from("com.amazonaws.s3#AmazonS3") | ||
|
||
override fun protocols(serviceId: ShapeId, currentProtocols: ProtocolMap): ProtocolMap { | ||
return currentProtocols.letIf(applies(serviceId)) { | ||
it + mapOf( | ||
RestXmlTrait.ID to RestXmlFactory { protocolConfig -> | ||
S3(protocolConfig) | ||
} | ||
) | ||
} | ||
} | ||
|
||
override fun libRsCustomizations( | ||
protocolConfig: ProtocolConfig, | ||
baseCustomizations: List<LibRsCustomization> | ||
): List<LibRsCustomization> { | ||
return baseCustomizations.letIf(applies(protocolConfig.serviceShape.id)) { | ||
it + S3PubUse() | ||
} | ||
} | ||
} | ||
|
||
class S3(protocolConfig: ProtocolConfig) : RestXml(protocolConfig) { | ||
private val runtimeConfig = protocolConfig.runtimeConfig | ||
override fun parseGenericError(operationShape: OperationShape): RuntimeType { | ||
return RuntimeType.forInlineFun("parse_generic_error", "xml_deser") { | ||
it.rustBlockTemplate( | ||
"pub fn parse_generic_error(response: &#{Response}<#{Bytes}>) -> Result<#{Error}, #{XmlError}>", | ||
"Response" to RuntimeType.http.member("Response"), | ||
"Bytes" to RuntimeType.Bytes, | ||
"Error" to RuntimeType.GenericError(runtimeConfig), | ||
"XmlError" to CargoDependency.smithyXml(runtimeConfig).asType().member("decode::XmlError") | ||
) { | ||
rustTemplate( | ||
""" | ||
let base_err = #{base_errors}::parse_generic_error(response.body().as_ref())?; | ||
Ok(#{s3_errors}::parse_extended_error(base_err, &response)) | ||
""", | ||
"base_errors" to restXmlErrors, "s3_errors" to AwsRuntimeType.S3Errors | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class S3PubUse : LibRsCustomization() { | ||
override fun section(section: LibRsSection): Writable = when (section) { | ||
is LibRsSection.Body -> writable { rust("pub use #T::ErrorExt;", AwsRuntimeType.S3Errors) } | ||
else -> emptySection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This Cargo.toml is unused in generated code. It exists solely to enable these tests to compile in-situ | ||
[package] | ||
name = "s3-tests" | ||
version = "0.1.0" | ||
authors = ["Russell Cohen <rcoh@amazon.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
aws-sdk-s3 = { path = "../../build/aws-sdk/s3" } | ||
smithy-http = { path = "../../build/aws-sdk/smithy-http" } | ||
http = "0.2.3" | ||
bytes = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ |
38 changes: 38 additions & 0 deletions
38
aws/sdk/integration-tests/s3/tests/custom-error-deserializer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
use aws_sdk_s3::operation::GetObject; | ||
use aws_sdk_s3::ErrorExt; | ||
use bytes::Bytes; | ||
use smithy_http::response::ParseHttpResponse; | ||
|
||
#[test] | ||
fn deserialize_extended_errors() { | ||
let resp = http::Response::builder() | ||
.header( | ||
"x-amz-id-2", | ||
"gyB+3jRPnrkN98ZajxHXr3u7EFM67bNgSAxexeEHndCX/7GRnfTXxReKUQF28IfP", | ||
) | ||
.header("x-amz-request-id", "3B3C7C725673C630") | ||
.status(404) | ||
.body( | ||
r#"<?xml version="1.0" encoding="UTF-8"?> | ||
<Error> | ||
<Code>NoSuchKey</Code> | ||
<Message>The resource you requested does not exist</Message> | ||
<Resource>/mybucket/myfoto.jpg</Resource> | ||
<RequestId>4442587FB7D0A2F9</RequestId> | ||
</Error>"#, | ||
) | ||
.unwrap(); | ||
let err = GetObject::new() | ||
.parse_loaded(&resp.map(Bytes::from)) | ||
.expect_err("status was 404, this is an error"); | ||
assert_eq!( | ||
err.meta().extended_request_id(), | ||
Some("gyB+3jRPnrkN98ZajxHXr3u7EFM67bNgSAxexeEHndCX/7GRnfTXxReKUQF28IfP") | ||
); | ||
assert_eq!(err.meta().request_id(), Some("4442587FB7D0A2F9")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.