Skip to content

Commit 89a2c00

Browse files
committed
Fix query compatible for JSON services
1 parent 1361161 commit 89a2c00

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

src/aws-cpp-sdk-core/source/client/AWSErrorMarshaller.cpp

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ AWSError<CoreErrors> JsonErrorMarshaller::Marshall(const Aws::Http::HttpResponse
6262

6363
if (httpResponse.HasHeader(ERROR_TYPE_HEADER)) {
6464
error = Marshall(httpResponse.GetHeader(ERROR_TYPE_HEADER), message);
65+
} else if (httpResponse.HasHeader(QUERY_ERROR_HEADER)) {
66+
auto errorCodeString = httpResponse.GetHeader(QUERY_ERROR_HEADER);
67+
auto locationOfSemicolon = errorCodeString.find_first_of(';');
68+
Aws::String errorCode;
69+
70+
if (locationOfSemicolon != Aws::String::npos) {
71+
errorCode = errorCodeString.substr(0, locationOfSemicolon);
72+
} else {
73+
errorCode = errorCodeString;
74+
}
75+
error = Marshall(errorCode, message);
6576
} else if (payloadView.ValueExists(TYPE)) {
6677
error = Marshall(payloadView.GetString(TYPE), message);
6778
} else {
@@ -256,45 +267,6 @@ AWSError<CoreErrors> AWSErrorMarshaller::FindErrorByHttpResponseCode(Aws::Http::
256267
}
257268

258269
void JsonErrorMarshallerQueryCompatible::MarshallError(AWSError<CoreErrors>& error, const Http::HttpResponse& httpResponse) const {
259-
if (!error.GetExceptionName().empty()) {
260-
auto exceptionPayload = GetJsonPayloadHttpResponse(httpResponse);
261-
auto payloadView = JsonView(exceptionPayload);
262-
/*
263-
AWS Query-Compatible mode: This is a special setting that allows
264-
certain AWS services to communicate using a specific "query"
265-
format, which can send customized error codes. Users are divided
266-
into different groups based on how they communicate with the
267-
service: Group #1: Users using the AWS Query format, receiving
268-
custom error codes. Group #2: Users using the regular AWS JSON
269-
format without the trait, receiving standard error codes. Group #3:
270-
Users using the AWS JSON format with the trait, receiving custom
271-
error codes.
272-
273-
The header "x-amzn-query-error" shouldn't be present if it's not
274-
awsQueryCompatible, so added checks for it.
275-
*/
276-
277-
if (httpResponse.HasHeader(QUERY_ERROR_HEADER)) {
278-
auto errorCodeString = httpResponse.GetHeader(QUERY_ERROR_HEADER);
279-
auto locationOfSemicolon = errorCodeString.find_first_of(';');
280-
Aws::String errorCode;
281-
282-
if (locationOfSemicolon != Aws::String::npos) {
283-
errorCode = errorCodeString.substr(0, locationOfSemicolon);
284-
} else {
285-
errorCode = errorCodeString;
286-
}
287-
288-
error.SetExceptionName(errorCode);
289-
}
290-
// check for exception name from payload field 'type'
291-
else if (payloadView.ValueExists(TYPE)) {
292-
// handle missing header and parse code from message
293-
const auto& typeStr = payloadView.GetString(TYPE);
294-
auto locationOfPound = typeStr.find_first_of('#');
295-
if (locationOfPound != Aws::String::npos) {
296-
error.SetExceptionName(typeStr.substr(locationOfPound + 1));
297-
}
298-
}
299-
}
270+
AWS_UNREFERENCED_PARAM(error);
271+
AWS_UNREFERENCED_PARAM(httpResponse);
300272
}

tests/aws-cpp-sdk-core-tests/aws/client/AWSErrorMashallerTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ TEST_F(AWSErrorMarshallerTest, TestErrorsWithPrefixParse)
477477
JsonErrorMarshallerQueryCompatible awsErrorMarshaller2;
478478
error = awsErrorMarshaller2.Marshall(
479479
*BuildHttpResponse(exceptionPrefix + "AccessDeniedException", message, requestId, LowerCaseMessage, "AwsQueryErrorCode"));
480-
ASSERT_EQ(CoreErrors::ACCESS_DENIED, error.GetErrorType());
480+
ASSERT_EQ(CoreErrors::UNKNOWN, error.GetErrorType());
481481
ASSERT_EQ("AwsQueryErrorCode", error.GetExceptionName());
482-
ASSERT_EQ(message, error.GetMessage());
482+
ASSERT_EQ("Unable to parse ExceptionName: AwsQueryErrorCode Message: Test Message", error.GetMessage());
483483
ASSERT_EQ(requestId, error.GetRequestId());
484484
ASSERT_FALSE(error.ShouldRetry());
485485
}
@@ -746,9 +746,9 @@ TEST_F(AWSErrorMarshallerTest, TestErrorsWithoutPrefixParse)
746746
JsonErrorMarshallerQueryCompatible awsErrorMarshaller2;
747747
error = awsErrorMarshaller2.Marshall(
748748
*BuildHttpResponse(exceptionPrefix + "AccessDeniedException", message, requestId, LowerCaseMessage, "AwsQueryErrorCode"));
749-
ASSERT_EQ(CoreErrors::ACCESS_DENIED, error.GetErrorType());
749+
ASSERT_EQ(CoreErrors::UNKNOWN, error.GetErrorType());
750750
ASSERT_EQ("AwsQueryErrorCode", error.GetExceptionName());
751-
ASSERT_EQ(message, error.GetMessage());
751+
ASSERT_EQ("Unable to parse ExceptionName: AwsQueryErrorCode Message: Test Message", error.GetMessage());
752752
ASSERT_EQ(requestId, error.GetRequestId());
753753
ASSERT_FALSE(error.ShouldRetry());
754754
}

tests/aws-cpp-sdk-monitoring-integration-tests/CloudWatchMonitoringTests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <aws/core/utils/memory/AWSMemory.h>
1111
#include <aws/monitoring/CloudWatchClient.h>
1212
#include <aws/monitoring/model/PutMetricDataRequest.h>
13+
#include <aws/monitoring/model/GetDashboardRequest.h>
1314
#include <aws/testing/AwsTestHelpers.h>
1415
#include <aws/testing/TestingEnvironment.h>
1516
#include <gtest/gtest.h>
@@ -91,4 +92,12 @@ TEST_F(CloudWatchMonitoringOperationTest, PutLargeMetricDataTest) {
9192
m_client->PutMetricData(request);
9293
AWS_ASSERT_SUCCESS(outcome);
9394
}
95+
96+
TEST_F(CloudWatchMonitoringOperationTest, DashboardNotFoundShouldParseCorrectly) {
97+
const auto response = m_client->GetDashboard(GetDashboardRequest().WithDashboardName("foo"));
98+
EXPECT_FALSE(response.IsSuccess());
99+
EXPECT_EQ(CloudWatchErrors::DASHBOARD_NOT_FOUND, response.GetError().GetErrorType());
100+
EXPECT_EQ("ResourceNotFound", response.GetError().GetExceptionName());
101+
EXPECT_EQ("Dashboard foo does not exist", response.GetError().GetMessage());
102+
}
94103
} // namespace

0 commit comments

Comments
 (0)