Search in sources :

Example 1 with XmlElement

use of software.amazon.awssdk.protocols.query.unmarshall.XmlElement in project aws-sdk-java-v2 by aws.

the class DecorateErrorFromResponseBodyUnmarshallerTest method status500_bodyWithError.

@Test
public void status500_bodyWithError() {
    DecorateErrorFromResponseBodyUnmarshaller decorateErrorFromResponseBodyUnmarshaller = DecorateErrorFromResponseBodyUnmarshaller.of(xml -> xml.getOptionalElementByName("test-error"));
    SdkHttpFullResponse sdkHttpFullResponse = SdkHttpFullResponse.builder().statusCode(500).build();
    XmlElement parsedError = XmlElement.builder().elementName("test-error").build();
    XmlElement parsedBody = XmlElement.builder().elementName("Error").addChildElement(parsedError).build();
    AwsXmlUnmarshallingContext context = AwsXmlUnmarshallingContext.builder().sdkHttpFullResponse(sdkHttpFullResponse).parsedXml(parsedBody).build();
    AwsXmlUnmarshallingContext result = decorateErrorFromResponseBodyUnmarshaller.apply(context);
    assertThat(result.isResponseSuccess()).isFalse();
    assertThat(result.parsedErrorXml()).isSameAs(parsedError);
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Test(org.junit.jupiter.api.Test)

Example 2 with XmlElement

use of software.amazon.awssdk.protocols.query.unmarshall.XmlElement in project aws-sdk-java-v2 by aws.

the class DecorateErrorFromResponseBodyUnmarshallerTest method status500_bodyWithNoError.

@Test
public void status500_bodyWithNoError() {
    DecorateErrorFromResponseBodyUnmarshaller decorateErrorFromResponseBodyUnmarshaller = DecorateErrorFromResponseBodyUnmarshaller.of(xml -> xml.getOptionalElementByName("test-error"));
    SdkHttpFullResponse sdkHttpFullResponse = SdkHttpFullResponse.builder().statusCode(500).build();
    XmlElement parsedBody = XmlElement.builder().elementName("ValidResponse").build();
    AwsXmlUnmarshallingContext context = AwsXmlUnmarshallingContext.builder().sdkHttpFullResponse(sdkHttpFullResponse).parsedXml(parsedBody).build();
    AwsXmlUnmarshallingContext result = decorateErrorFromResponseBodyUnmarshaller.apply(context);
    assertThat(result.isResponseSuccess()).isFalse();
    assertThat(result.parsedErrorXml()).isNull();
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Test(org.junit.jupiter.api.Test)

Example 3 with XmlElement

use of software.amazon.awssdk.protocols.query.unmarshall.XmlElement in project aws-sdk-java-v2 by aws.

the class DecorateErrorFromResponseBodyUnmarshallerTest method status200_bodyWithNoError.

@Test
public void status200_bodyWithNoError() {
    DecorateErrorFromResponseBodyUnmarshaller decorateErrorFromResponseBodyUnmarshaller = DecorateErrorFromResponseBodyUnmarshaller.of(FAIL_TEST_ERROR_ROOT_LOCATOR);
    SdkHttpFullResponse sdkHttpFullResponse = SdkHttpFullResponse.builder().statusCode(200).build();
    XmlElement parsedBody = XmlElement.builder().elementName("ValidResponse").build();
    AwsXmlUnmarshallingContext context = AwsXmlUnmarshallingContext.builder().sdkHttpFullResponse(sdkHttpFullResponse).parsedXml(parsedBody).build();
    AwsXmlUnmarshallingContext result = decorateErrorFromResponseBodyUnmarshaller.apply(context);
    assertThat(result.isResponseSuccess()).isTrue();
    assertThat(result.parsedErrorXml()).isNull();
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Test(org.junit.jupiter.api.Test)

Example 4 with XmlElement

use of software.amazon.awssdk.protocols.query.unmarshall.XmlElement in project aws-sdk-java-v2 by aws.

the class DecorateErrorFromResponseBodyUnmarshallerTest method status200_bodyWithError.

@Test
public void status200_bodyWithError() {
    DecorateErrorFromResponseBodyUnmarshaller decorateErrorFromResponseBodyUnmarshaller = DecorateErrorFromResponseBodyUnmarshaller.of(FAIL_TEST_ERROR_ROOT_LOCATOR);
    SdkHttpFullResponse sdkHttpFullResponse = SdkHttpFullResponse.builder().statusCode(200).build();
    XmlElement parsedError = XmlElement.builder().elementName("test-error").build();
    XmlElement parsedBody = XmlElement.builder().elementName("Error").addChildElement(parsedError).build();
    AwsXmlUnmarshallingContext context = AwsXmlUnmarshallingContext.builder().sdkHttpFullResponse(sdkHttpFullResponse).parsedXml(parsedBody).build();
    AwsXmlUnmarshallingContext result = decorateErrorFromResponseBodyUnmarshaller.apply(context);
    assertThat(result.isResponseSuccess()).isFalse();
    assertThat(result.parsedErrorXml()).isSameAs(parsedBody);
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Test(org.junit.jupiter.api.Test)

Example 5 with XmlElement

use of software.amazon.awssdk.protocols.query.unmarshall.XmlElement in project aws-sdk-java-v2 by aws.

the class AwsXmlErrorUnmarshaller method unmarshall.

/**
 * Unmarshal an AWS XML exception
 * @param documentRoot The parsed payload document
 * @param errorRoot The specific element of the parsed payload document that contains the error to be marshalled
 *                  or empty if it could not be located.
 * @param documentBytes The raw bytes of the original payload document if they are available
 * @param response The HTTP response object
 * @param executionAttributes {@link ExecutionAttributes} for the current execution
 * @return An {@link AwsServiceException} unmarshalled from the XML.
 */
public AwsServiceException unmarshall(XmlElement documentRoot, Optional<XmlElement> errorRoot, Optional<SdkBytes> documentBytes, SdkHttpFullResponse response, ExecutionAttributes executionAttributes) {
    String errorCode = getErrorCode(errorRoot);
    AwsServiceException.Builder builder = errorRoot.map(e -> invokeSafely(() -> unmarshallFromErrorCode(response, e, errorCode))).orElseGet(this::defaultException);
    AwsErrorDetails awsErrorDetails = AwsErrorDetails.builder().errorCode(errorCode).errorMessage(builder.message()).rawResponse(documentBytes.orElse(null)).sdkHttpResponse(response).serviceName(executionAttributes.getAttribute(AwsExecutionAttribute.SERVICE_NAME)).build();
    builder.requestId(getRequestId(response, documentRoot)).extendedRequestId(getExtendedRequestId(response)).statusCode(response.statusCode()).clockSkew(getClockSkew(executionAttributes)).awsErrorDetails(awsErrorDetails);
    return builder.build();
}
Also used : ExceptionMetadata(software.amazon.awssdk.protocols.core.ExceptionMetadata) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) SdkExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkExecutionAttribute) AwsExecutionAttribute(software.amazon.awssdk.awscore.AwsExecutionAttribute) Supplier(java.util.function.Supplier) SdkBytes(software.amazon.awssdk.core.SdkBytes) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) List(java.util.List) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Duration(java.time.Duration) AwsErrorDetails(software.amazon.awssdk.awscore.exception.AwsErrorDetails) XmlErrorUnmarshaller(software.amazon.awssdk.protocols.query.unmarshall.XmlErrorUnmarshaller) SdkPojo(software.amazon.awssdk.core.SdkPojo) Optional(java.util.Optional) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) HttpResponseHandler(software.amazon.awssdk.core.http.HttpResponseHandler) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) AwsErrorDetails(software.amazon.awssdk.awscore.exception.AwsErrorDetails)

Aggregations

XmlElement (software.amazon.awssdk.protocols.query.unmarshall.XmlElement)12 Test (org.junit.jupiter.api.Test)7 SdkHttpFullResponse (software.amazon.awssdk.http.SdkHttpFullResponse)6 HashMap (java.util.HashMap)4 SdkField (software.amazon.awssdk.core.SdkField)3 StringInputStream (software.amazon.awssdk.utils.StringInputStream)3 List (java.util.List)2 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)2 SdkBytes (software.amazon.awssdk.core.SdkBytes)2 SdkPojo (software.amazon.awssdk.core.SdkPojo)2 MapTrait (software.amazon.awssdk.core.traits.MapTrait)2 XmlErrorUnmarshaller (software.amazon.awssdk.protocols.query.unmarshall.XmlErrorUnmarshaller)2 FunctionalUtils.invokeSafely (software.amazon.awssdk.utils.FunctionalUtils.invokeSafely)2 Duration (java.time.Duration)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 AwsExecutionAttribute (software.amazon.awssdk.awscore.AwsExecutionAttribute)1 AwsErrorDetails (software.amazon.awssdk.awscore.exception.AwsErrorDetails)1 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)1