Skip to content

Commit

Permalink
limits deriving error code from status to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Oct 15, 2020
1 parent c80253c commit 24a8806
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,13 @@ public static void writeXmlErrorMessageCodeDeserializer(ProtocolGenerator.Genera

if (requiresS3Customization(service)) {
writer.addUseImports(AwsCustomGoDependency.S3_SHARED_CUSTOMIZATION);
writer.write("errorComponents, err := s3shared.GetErrorResponseComponents(errorBody, response.StatusCode)");
String getErrorComponents = String.format("errorComponents, err := "
+ "s3shared.GetErrorResponseComponents(errorBody, response.StatusCode, %s)",
isS3Service(service));
writer.write(getErrorComponents);

writer.write("if err != nil { return err }");

writer.insertTrailingNewline();
writer.openBlock("if hostID := errorComponents.HostID; len(hostID)!=0 {", "}", () -> {
writer.write("s3shared.SetHostIDMetadata(metadata, hostID)");
Expand Down Expand Up @@ -307,6 +312,11 @@ private static boolean requiresS3Customization(ServiceShape service) {
String serviceId= service.expectTrait(ServiceTrait.class).getSdkId();
return serviceId.equalsIgnoreCase("S3") || serviceId.equalsIgnoreCase("S3 Control");
}

private static boolean isS3Service(ServiceShape service) {
String serviceId= service.expectTrait(ServiceTrait.class).getSdkId();
return serviceId.equalsIgnoreCase("S3");
}
}


5 changes: 3 additions & 2 deletions service/internal/s3shared/xml_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ type ErrorComponents struct {
}

// GetErrorResponseComponents returns the error fields from an xml error response body
func GetErrorResponseComponents(r io.Reader, statusCode int) (ErrorComponents, error) {
func GetErrorResponseComponents(r io.Reader, statusCode int, isS3service bool) (ErrorComponents, error) {
var errComponents ErrorComponents
if err := xml.NewDecoder(r).Decode(&errComponents); err != nil && err != io.EOF {
return ErrorComponents{}, fmt.Errorf("error while deserializing xml error response : %w", err)
}

if len(errComponents.Code) == 0 && len(errComponents.Message) == 0 {
// for S3 service, we derive err code and message, if none is found
if isS3service && len(errComponents.Code) == 0 && len(errComponents.Message) == 0 {
// derive code and message from status code
statusText := http.StatusText(statusCode)
errComponents.Code = strings.Replace(statusText, " ", "", -1)
Expand Down
14 changes: 11 additions & 3 deletions service/internal/s3shared/xml_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func TestGetResponseErrorCode(t *testing.T) {
cases := map[string]struct {
isS3Service bool
status int
errorResponse io.Reader
expectedErrorCode string
Expand All @@ -17,7 +18,8 @@ func TestGetResponseErrorCode(t *testing.T) {
expectedErrorHostID string
}{
"standard xml error": {
status: 400,
isS3Service: true,
status: 400,
errorResponse: bytes.NewReader([]byte(`<Error>
<Type>Sender</Type>
<Code>InvalidGreeting</Code>
Expand All @@ -30,17 +32,23 @@ func TestGetResponseErrorCode(t *testing.T) {
expectedErrorRequestID: "foo-id",
expectedErrorHostID: "bar-id",
},
"no response body": {
"s3 no response body": {
isS3Service: true,
status: 400,
errorResponse: bytes.NewReader([]byte(``)),
expectedErrorCode: "BadRequest",
expectedErrorMessage: "Bad Request",
},
"s3control no response body": {
isS3Service: false,
status: 400,
errorResponse: bytes.NewReader([]byte(``)),
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
ec, err := GetErrorResponseComponents(c.errorResponse, c.status)
ec, err := GetErrorResponseComponents(c.errorResponse, c.status, c.isS3Service)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand Down

0 comments on commit 24a8806

Please sign in to comment.