Java Code Examples for software.amazon.awssdk.core.interceptor.Context#FailedExecution

The following examples show how to use software.amazon.awssdk.core.interceptor.Context#FailedExecution . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelpfulUnknownHostExceptionInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public Throwable modifyException(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    if (!hasCause(context.exception(), UnknownHostException.class)) {
        return context.exception();
    }

    StringBuilder error = new StringBuilder();
    error.append("Received an UnknownHostException when attempting to interact with a service. See cause for the "
                 + "exact endpoint that is failing to resolve. ");

    Optional<String> globalRegionErrorDetails = getGlobalRegionErrorDetails(executionAttributes);

    if (globalRegionErrorDetails.isPresent()) {
        error.append(globalRegionErrorDetails.get());
    } else {
        error.append("If this is happening on an endpoint that previously worked, there may be a network connectivity "
                     + "issue or your DNS cache could be storing endpoints for too long.");
    }

    return SdkClientException.builder().message(error.toString()).cause(context.exception()).build();
}
 
Example 2
Source File: ExecutionInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void verifyFailedExecutionMethodCalled(ArgumentCaptor<Context.FailedExecution> failedExecutionArg,
                                               boolean expectResponse) {
    MembersInHeadersRequest failedRequest = (MembersInHeadersRequest) failedExecutionArg.getValue().request();

    assertThat(failedRequest.stringMember()).isEqualTo("1");
    assertThat(failedExecutionArg.getValue().httpRequest()).hasValueSatisfying(httpRequest -> {
        assertThat(httpRequest.firstMatchingHeader("x-amz-string")).hasValue("1");
        assertThat(httpRequest.firstMatchingHeader("x-amz-integer")).hasValue("2");
    });
    assertThat(failedExecutionArg.getValue().httpResponse()).hasValueSatisfying(httpResponse -> {
        assertThat(httpResponse.firstMatchingHeader("x-amz-integer")).hasValue("3");
    });

    if (expectResponse) {
        assertThat(failedExecutionArg.getValue().response().map(MembersInHeadersResponse.class::cast)).hasValueSatisfying(response -> {
            assertThat(response.integerMember()).isEqualTo(3);
            assertThat(response.stringMember()).isEqualTo("4");
        });
    } else {
        assertThat(failedExecutionArg.getValue().response()).isNotPresent();
    }
}
 
Example 3
Source File: ExecutionInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void expectServiceCallErrorMethodsCalled(ExecutionInterceptor interceptor, boolean isAsync) {
    ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class);
    ArgumentCaptor<Context.BeforeUnmarshalling> beforeUnmarshallingArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class);
    ArgumentCaptor<Context.FailedExecution> failedExecutionArg = ArgumentCaptor.forClass(Context.FailedExecution.class);

    InOrder inOrder = Mockito.inOrder(interceptor);
    inOrder.verify(interceptor).beforeExecution(any(), attributes.capture());
    inOrder.verify(interceptor).modifyRequest(any(), attributes.capture());
    inOrder.verify(interceptor).beforeMarshalling(any(), attributes.capture());
    inOrder.verify(interceptor).afterMarshalling(any(), attributes.capture());
    inOrder.verify(interceptor).modifyAsyncHttpContent(any(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpContent(any(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpRequest(any(), attributes.capture());
    inOrder.verify(interceptor).beforeTransmission(any(), attributes.capture());
    inOrder.verify(interceptor).afterTransmission(any(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpResponse(any(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpResponseContent(any(), attributes.capture());
    inOrder.verify(interceptor).beforeUnmarshalling(beforeUnmarshallingArg.capture(), attributes.capture());
    if (isAsync) {
        inOrder.verify(interceptor).modifyAsyncHttpResponseContent(any(), attributes.capture());
    }
    inOrder.verify(interceptor).modifyException(failedExecutionArg.capture(), attributes.capture());
    inOrder.verify(interceptor).onExecutionFailure(failedExecutionArg.capture(), attributes.capture());
    verifyNoMoreInteractions(interceptor);

    // Verify same execution attributes were used for all method calls
    assertThat(attributes.getAllValues()).containsOnly(attributes.getAllValues().get(0));

    // Verify HTTP response
    assertThat(beforeUnmarshallingArg.getValue().httpResponse().statusCode()).isEqualTo(404);

    // Verify failed execution parameters
    assertThat(failedExecutionArg.getValue().exception()).isInstanceOf(SdkServiceException.class);
    verifyFailedExecutionMethodCalled(failedExecutionArg, false);
}
 
Example 4
Source File: SpectatorExecutionInterceptorTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
Context.FailedExecution failureContext() {
  return new Context.FailedExecution() {
    @Override
    public Throwable exception() {
      return error;
    }

    @Override
    public SdkRequest request() {
      return null;
    }

    @Override
    public Optional<SdkHttpRequest> httpRequest() {
      return Optional.ofNullable(request);
    }

    @Override
    public Optional<SdkHttpResponse> httpResponse() {
      return Optional.ofNullable(response);
    }

    @Override
    public Optional<SdkResponse> response() {
      return Optional.empty();
    }
  };
}
 
Example 5
Source File: TracingExecutionInterceptor.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code null} when there's neither a cause nor an AWS error message. This assumes it was
 * a plain HTTP status failure.
 */
@Nullable static Throwable maybeError(Context.FailedExecution context) {
  Throwable error = context.exception();
  if (error.getCause() == null && error instanceof AwsServiceException) {
    AwsServiceException serviceException = (AwsServiceException) error;
    if (serviceException.awsErrorDetails().errorMessage() == null) {
      return null;
    }
  }
  return error;
}
 
Example 6
Source File: TracingExecutionInterceptor.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
/**
 * After a SDK request has failed
 */
@Override public void onExecutionFailure(
    Context.FailedExecution context,
    ExecutionAttributes executionAttributes
) {
  Span span = executionAttributes.getAttribute(SPAN);
  if (span == null) {
    // An evil interceptor deleted our attribute.
    return;
  }
  handler.handleReceive(new HttpClientResponse(
      context.httpRequest().orElse(null),
      context.httpResponse().orElse(null),
      maybeError(context)), span);
}
 
Example 7
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionFailure(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    Subsegment subsegment = executionAttributes.getAttribute(entityKey);
    if (subsegment == null) {
        return;
    }

    populateSubsegmentException(subsegment, context);
    populateRequestId(subsegment, context);
    if (context.httpResponse().isPresent()) {
        populateSubsegmentWithResponse(subsegment, context.httpResponse().get());
    }

    getRecorder().endSubsegment(subsegment);
}
 
Example 8
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void otherRequest_shouldNotThrowException() {
    S3Exception s3Exception = create404S3Exception();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 PutObjectRequest.builder().build());
    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes())).isEqualTo(s3Exception);
}
 
Example 9
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headObjectOtherException_shouldNotThrowException() {
    S3Exception s3Exception = (S3Exception) S3Exception.builder().statusCode(500).build();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadObjectRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes())).isEqualTo(s3Exception);
}
 
Example 10
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headObject404_shouldTranslateException() {
    S3Exception s3Exception = create404S3Exception();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadObjectRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes()))
        .isExactlyInstanceOf(NoSuchKeyException.class);
}
 
Example 11
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headBucket404_shouldTranslateException() {
    S3Exception s3Exception = create404S3Exception();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadBucketRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes()))
        .isExactlyInstanceOf(NoSuchBucketException.class);
}
 
Example 12
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
private void populateSubsegmentException(Subsegment subsegment, Context.FailedExecution context) {
    Throwable exception = context.exception();
    subsegment.addException(exception);

    int statusCode = -1;
    if (exception instanceof SdkServiceException) {
        statusCode = ((SdkServiceException) exception).statusCode();
        subsegment.getCause().setMessage(exception.getMessage());
        if (((SdkServiceException) exception).isThrottlingException()) {
            subsegment.setThrottle(true);
            // throttling errors are considered client-side errors
            subsegment.setError(true);
        }
        setRemoteForException(subsegment, exception);
    } else if (context.httpResponse().isPresent()) {
        statusCode = context.httpResponse().get().statusCode();
    }

    if (statusCode == -1) {
        return;
    }

    if (statusCode >= 400 && statusCode < 500) {
        subsegment.setFault(false);
        subsegment.setError(true);
        if (statusCode == 429) {
            subsegment.setThrottle(true);
        }
    } else if (statusCode >= 500) {
        subsegment.setFault(true);
    }
}
 
Example 13
Source File: ExecutionInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void expectAllMethodsCalled(ExecutionInterceptor interceptor,
                                    SdkRequest inputRequest,
                                    Exception expectedException,
                                    boolean isAsync) {
    ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class);

    ArgumentCaptor<Context.BeforeExecution> beforeExecutionArg = ArgumentCaptor.forClass(Context.BeforeExecution.class);
    ArgumentCaptor<Context.BeforeMarshalling> modifyRequestArg = ArgumentCaptor.forClass(Context.BeforeMarshalling.class);
    ArgumentCaptor<Context.BeforeMarshalling> beforeMarshallingArg = ArgumentCaptor.forClass(Context.BeforeMarshalling.class);
    ArgumentCaptor<Context.AfterMarshalling> afterMarshallingArg = ArgumentCaptor.forClass(Context.AfterMarshalling.class);
    ArgumentCaptor<Context.BeforeTransmission> modifyHttpRequestArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    ArgumentCaptor<Context.BeforeTransmission> modifyHttpContentArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    ArgumentCaptor<Context.BeforeTransmission> modifyHttpContentAsyncArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    ArgumentCaptor<Context.BeforeTransmission> beforeTransmissionArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    ArgumentCaptor<Context.AfterTransmission> afterTransmissionArg = ArgumentCaptor.forClass(Context.AfterTransmission.class);
    ArgumentCaptor<Context.BeforeUnmarshalling> modifyHttpResponseArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class);
    ArgumentCaptor<Context.BeforeUnmarshalling> modifyHttpResponseContentArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class);
    ArgumentCaptor<Context.BeforeUnmarshalling> beforeUnmarshallingArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class);
    ArgumentCaptor<Context.BeforeUnmarshalling> modifyAsyncHttpResponseContent = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class);
    ArgumentCaptor<Context.AfterUnmarshalling> afterUnmarshallingArg = ArgumentCaptor.forClass(Context.AfterUnmarshalling.class);
    ArgumentCaptor<Context.AfterExecution> modifyResponseArg = ArgumentCaptor.forClass(Context.AfterExecution.class);
    ArgumentCaptor<Context.AfterExecution> afterExecutionArg = ArgumentCaptor.forClass(Context.AfterExecution.class);

    // Verify methods are called in the right order
    InOrder inOrder = Mockito.inOrder(interceptor);
    inOrder.verify(interceptor).beforeExecution(beforeExecutionArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyRequest(modifyRequestArg.capture(), attributes.capture());
    inOrder.verify(interceptor).beforeMarshalling(beforeMarshallingArg.capture(), attributes.capture());
    inOrder.verify(interceptor).afterMarshalling(afterMarshallingArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyAsyncHttpContent(modifyHttpContentAsyncArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpContent(modifyHttpContentArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpRequest(modifyHttpRequestArg.capture(), attributes.capture());
    inOrder.verify(interceptor).beforeTransmission(beforeTransmissionArg.capture(), attributes.capture());
    inOrder.verify(interceptor).afterTransmission(afterTransmissionArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpResponse(modifyHttpResponseArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyHttpResponseContent(modifyHttpResponseContentArg.capture(), attributes.capture());
    inOrder.verify(interceptor).beforeUnmarshalling(beforeUnmarshallingArg.capture(), attributes.capture());
    if (isAsync) {
        inOrder.verify(interceptor).modifyAsyncHttpResponseContent(modifyAsyncHttpResponseContent.capture(), attributes.capture());
    }
    inOrder.verify(interceptor).afterUnmarshalling(afterUnmarshallingArg.capture(), attributes.capture());
    inOrder.verify(interceptor).modifyResponse(modifyResponseArg.capture(), attributes.capture());
    inOrder.verify(interceptor).afterExecution(afterExecutionArg.capture(), attributes.capture());
    if (expectedException != null) {
        ArgumentCaptor<Context.FailedExecution> failedExecutionArg = ArgumentCaptor.forClass(Context.FailedExecution.class);
        inOrder.verify(interceptor).modifyException(failedExecutionArg.capture(), attributes.capture());
        inOrder.verify(interceptor).onExecutionFailure(failedExecutionArg.capture(), attributes.capture());
        verifyFailedExecutionMethodCalled(failedExecutionArg, true);
        assertThat(failedExecutionArg.getValue().exception()).isEqualTo(expectedException);
    }
    verifyNoMoreInteractions(interceptor);

    // Verify beforeExecution gets untouched request
    assertThat(beforeExecutionArg.getValue().request()).isSameAs(inputRequest);

    // Verify methods were given correct parameters
    validateArgs(beforeExecutionArg.getValue(), null);
    validateArgs(modifyRequestArg.getValue(), null);
    validateArgs(beforeMarshallingArg.getValue(), "1");
    validateArgs(afterMarshallingArg.getValue(), "1", null);
    validateArgs(modifyHttpRequestArg.getValue(), "1", null);
    validateArgs(beforeTransmissionArg.getValue(), "1", "2");
    validateArgs(afterTransmissionArg.getValue(), "1", "2", null);
    validateArgs(modifyHttpResponseArg.getValue(), "1", "2", null);
    validateArgs(beforeUnmarshallingArg.getValue(), "1", "2", "3");
    validateArgs(afterUnmarshallingArg.getValue(), "1", "2", "3", null);
    validateArgs(modifyResponseArg.getValue(), "1", "2", "3", null);
    validateArgs(afterExecutionArg.getValue(), "1", "2", "3", "4");

    // Verify same execution attributes were used for all method calls
    assertThat(attributes.getAllValues()).containsOnly(attributes.getAllValues().get(0));
}
 
Example 14
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private Context.FailedExecution getFailedExecution(S3Exception s3Exception, SdkRequest sdkRequest) {
    return DefaultFailedExecutionContext.builder().interceptorContext(InterceptorContext.builder()
                                                                                        .request(sdkRequest)
                                                                                        .build()).exception(s3Exception).build();
}
 
Example 15
Source File: SlowExecutionInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void onExecutionFailure(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    wait(onExecutionFailureWait);
}
 
Example 16
Source File: ExceptionReportingUtilsTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void onExecutionFailure(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    throw new RuntimeException("OOPS");
}
 
Example 17
Source File: ExceptionReportingUtilsTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Throwable modifyException(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    return exceptionToThrow;
}
 
Example 18
Source File: GlobalServiceExecutionInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Throwable modifyException(Context.FailedExecution context, ExecutionAttributes executionAttributes) {
    return DELEGATE.modifyException(context, executionAttributes);
}
 
Example 19
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
private void populateRequestId(Subsegment subsegment, Context.FailedExecution context) {
    populateRequestId(subsegment, context.response(), context.httpResponse(), context.exception());
}