Java源码示例:software.amazon.awssdk.core.exception.SdkException

示例1
@Test
public void handleRequest_FailedCreate_UnknownError() {
    final CreateHandler handler = new CreateHandler();

    doThrow(SdkException.builder().message("test error").build())
        .when(proxy)
        .injectCredentialsAndInvokeV2(
            any(),
            any()
        );

    final ResourceModel model = ResourceModel.builder()
        .name("test-set")
        .build();

    final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder()
        .desiredResourceState(model)
        .build();

    assertThrows(SdkException.class, () -> {
        handler.handleRequest(proxy, request, null, logger);
    });
}
 
示例2
@Test
public void handleRequest_FailedDelete_UnknownError() {
    final DeleteHandler handler = new DeleteHandler();

    // all Exceptions should be thrown so they can be handled by wrapper
    doThrow(SdkException.builder().message("test error").build())
        .when(proxy)
        .injectCredentialsAndInvokeV2(
            ArgumentMatchers.any(),
            ArgumentMatchers.any()
        );

    final ResourceModel model = ResourceModel.builder()
        .name("test-set")
        .build();

    final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder()
        .desiredResourceState(model)
        .build();

    assertThrows(SdkException.class, () -> {
        handler.handleRequest(proxy, request, null, logger);
    });
}
 
示例3
private synchronized Metadata getMetadata() throws IOException {
    if (resolved) {
        return metadata;
    }

    try {
        resolved = true;
        Artifact artifact = listFiles();
        if (artifact == null) {
            logger.debug("No object found in s3 bucket.");
            return null;
        }

        metadata = new Metadata.MatchAllMetadata();
        String hash = md5hash("s3://" + bucket + '/' + prefix);
        MRL mrl = MRL.model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
        metadata.setRepositoryUri(mrl.toURI());
        metadata.setArtifactId(artifactId);
        metadata.setArtifacts(Collections.singletonList(artifact));
        return metadata;
    } catch (SdkException e) {
        throw new IOException("Failed scan s3 bucket: " + bucket, e);
    }
}
 
示例4
@Test
public void legacyRetryModeExcludesThrottlingExceptions() throws InterruptedException {
    stubThrottlingResponse();

    ExecutorService executor = Executors.newFixedThreadPool(51);
    ClientT client = clientBuilder().overrideConfiguration(o -> o.retryPolicy(RetryMode.LEGACY)).build();

    for (int i = 0; i < 51; ++i) {
        executor.execute(() -> assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class));
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();

    // 51 requests * 4 attempts = 204 requests
    verifyRequestCount(204);
}
 
示例5
@Test
public void standardRetryModeIncludesThrottlingExceptions() throws InterruptedException {
    stubThrottlingResponse();

    ExecutorService executor = Executors.newFixedThreadPool(51);
    ClientT client = clientBuilder().overrideConfiguration(o -> o.retryPolicy(RetryMode.STANDARD)).build();

    for (int i = 0; i < 51; ++i) {
        executor.execute(() -> assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class));
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();

    // Would receive 153 without throttling (51 requests * 3 attempts = 153 requests)
    verifyRequestCount(151);
}
 
示例6
/**
 * Combines and decorates separate success and failure response handlers into a single combined response handler
 * that handles both cases and produces a {@link Response} object that wraps the result. The handlers are
 * decorated with additional behavior (such as CRC32 validation).
 */
private <OutputT extends SdkResponse> TransformingAsyncResponseHandler<Response<OutputT>> createDecoratedHandler(
    HttpResponseHandler<OutputT> successHandler,
    HttpResponseHandler<? extends SdkException> errorHandler,
    ExecutionContext executionContext) {

    HttpResponseHandler<OutputT> decoratedResponseHandlers =
        decorateResponseHandlers(successHandler, executionContext);

    TransformingAsyncResponseHandler<OutputT> decoratedSuccessHandler =
        new AsyncResponseHandler<>(decoratedResponseHandlers,
                                   crc32Validator,
                                   executionContext.executionAttributes());

    TransformingAsyncResponseHandler<? extends SdkException> decoratedErrorHandler =
        resolveErrorResponseHandler(errorHandler, executionContext, crc32Validator);
    return new CombinedResponseAsyncHttpResponseHandler<>(decoratedSuccessHandler, decoratedErrorHandler);
}
 
示例7
/**
 * @param context Context about the state of the last request and information about the number of requests made.
 * @return True if the exception class matches one of the whitelisted exceptions or if the cause of the exception matches the
 *     whitelisted exception.
 */
@Override
public boolean shouldRetry(RetryPolicyContext context) {

    SdkException exception = context.exception();
    if (exception == null) {
        return false;
    }

    Predicate<Class<? extends Exception>> isRetryableException =
        ex -> ex.isAssignableFrom(exception.getClass());

    Predicate<Class<? extends Exception>> hasRetrableCause =
        ex -> exception.getCause() != null && ex.isAssignableFrom(exception.getCause().getClass());

    return exceptionsToRetryOn.stream().anyMatch(isRetryableException.or(hasRetrableCause));
}
 
示例8
private String getSecret(String[] arnTokens) throws IOException {
  String region = arnTokens[Arn.region.ordinal()];
  String secretName = getSecretName(arnTokens[Arn.secretName.ordinal()]);

  /*
   * Currently, dremio would support access of the secrets manager with base role assigned
   * to EC2 machine. This will be further enhanced, once we have more requirements on it.
   */
  AwsCredentialsProvider awsCredentialsProvider = getAwsCredentials();
  GetSecretValueRequest secretValueRequest = GetSecretValueRequest.builder().secretId(secretName)
    .versionStage(AWS_CURRENT).build();

  try (final SecretsManagerClient secretsManagerClient = SecretsManagerClient.builder()
        .region(Region.of(region))
        .credentialsProvider(awsCredentialsProvider)
        .build()) {
    final GetSecretValueResponse secretValueResponse = secretsManagerClient.getSecretValue(secretValueRequest);
    return (secretValueResponse.secretString() != null) ?
      secretValueResponse.secretString() : secretValueResponse.secretBinary().toString();
  } catch (SdkException e) {
    logger.debug("Unable to retrieve secret for secret {} as {}", secretName, e.getMessage());
    throw new IOException(e.getMessage(), e);
  }
}
 
示例9
public static void main(String[] args) {
    try {
        // snippet-start:[mediaconvert.java.getendpointurl.build_mediaconvertclient]
        MediaConvertClient mc = MediaConvertClient.builder().build();
        // snippet-end:[mediaconvert.java.getendpointurl.build_mediaconvertclient]
        // snippet-start:[mediaconvert.java.getendpointurl.retrieve_endpoints]
        DescribeEndpointsResponse res = mc
                .describeEndpoints(DescribeEndpointsRequest.builder().maxResults(20).build());

        Iterator<Endpoint> endpoints = res.endpoints().iterator();
        while (endpoints.hasNext()) {
            System.out.println(endpoints.next().url());
        }
        // snippet-end:[mediaconvert.java.getendpointurl.retrieve_endpoints]
    } catch (SdkException e) {
        System.out.println(e.toString());
    } finally {
    }
}
 
示例10
/**
 * Creates a new SQS queue on AWS.
 * @param name queue name
 * @return the queue URL or null
 */
protected static String createQueue(String name) {
	if (StringUtils.isBlank(name)) {
		return null;
	}
	String queueURL = getQueueURL(name);
	if (queueURL == null) {
		try {
			queueURL = getClient().createQueue(b -> b.queueName(name)).get().queueUrl();
		} catch (AwsServiceException ase) {
			logException(ase);
		} catch (SdkException ace) {
			logger.error("Could not reach SQS. {0}", ace.toString());
		} catch (InterruptedException | ExecutionException ex) {
			logger.error(null, ex);
			Thread.currentThread().interrupt();
		}
	}
	return queueURL;
}
 
示例11
@Test
public void testGetRecordsThrowsSdkException() throws Exception {
    expectedExceptionRule.expect(SdkException.class);
    expectedExceptionRule.expectMessage("Test Exception");

    CompletableFuture<GetShardIteratorResponse> getShardIteratorFuture = CompletableFuture
            .completedFuture(GetShardIteratorResponse.builder().shardIterator("test").build());

    // Set up proxy mock methods
    when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class))).thenReturn(getShardIteratorFuture);
    when(kinesisClient.getRecords(any(GetRecordsRequest.class))).thenReturn(getRecordsResponseFuture);
    when(getRecordsResponseFuture.get(anyLong(), any(TimeUnit.class)))
            .thenThrow(new ExecutionException(SdkException.builder().message("Test Exception").build()));

    // Create data fectcher and initialize it with latest type checkpoint
    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);

    // Call records of dataFetcher which will throw an exception
    getRecordsRetrievalStrategy.getRecords(MAX_RECORDS);

}
 
示例12
private void callApi(DynamoDbClient client) {
    try {
        client.listTables(ListTablesRequest.builder().build());
    } catch (SdkException expected) {
        // Ignored or expected.
    }
}
 
示例13
@Test(timeout = 10_000)
public void canBeEnabledViaProfileOnOverrideConfiguration() throws InterruptedException {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);

    String profileFileContent =
        "[default]\n" +
        "aws_endpoint_discovery_enabled = true";

    ProfileFile profileFile = ProfileFile.builder()
                                         .type(ProfileFile.Type.CONFIGURATION)
                                         .content(new StringInputStream(profileFileContent))
                                         .build();

    DynamoDbClient dynamoDb = DynamoDbClient.builder()
                                            .region(Region.US_WEST_2)
                                            .credentialsProvider(AnonymousCredentialsProvider.create())
                                            .overrideConfiguration(c -> c.defaultProfileFile(profileFile)
                                                                         .defaultProfileName("default")
                                                                         .addExecutionInterceptor(interceptor)
                                                                         .retryPolicy(r -> r.numRetries(0)))
                                            .build();

    assertThatThrownBy(dynamoDb::listTables).isInstanceOf(SdkException.class);

    ArgumentCaptor<Context.BeforeTransmission> context;

    do {
        Thread.sleep(1);
        context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
        Mockito.verify(interceptor, atLeastOnce()).beforeTransmission(context.capture(), any());
    } while (context.getAllValues().size() < 2);

    assertThat(context.getAllValues()
                      .stream()
                      .anyMatch(v -> v.httpRequest()
                                      .firstMatchingHeader("X-Amz-Target")
                                      .map(h -> h.equals("DynamoDB_20120810.DescribeEndpoints"))
                                      .orElse(false)))
        .isTrue();
}
 
示例14
/**
 * Returns the URL for an object stored in Amazon S3.
 *
 * If the object identified by the given bucket and key has public read permissions,
 * then this URL can be directly accessed to retrieve the object's data.
 *
 * <p>
 *     If same configuration options are set on both #GetUrlRequest and #S3Utilities objects (for example: region),
 *     the configuration set on the #GetUrlRequest takes precedence.
 * </p>
 *
 * @param getUrlRequest request to construct url
 * @return A URL for an object stored in Amazon S3.
 * @throws MalformedURLException Generated Url is malformed
 */
public URL getUrl(GetUrlRequest getUrlRequest) {
    Region resolvedRegion = resolveRegionForGetUrl(getUrlRequest);
    URI resolvedEndpoint = resolveEndpoint(getUrlRequest.endpoint(), resolvedRegion);
    boolean endpointOverridden = getUrlRequest.endpoint() != null;

    SdkHttpFullRequest marshalledRequest = createMarshalledRequest(getUrlRequest, resolvedEndpoint);

    GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                                                        .bucket(getUrlRequest.bucket())
                                                        .key(getUrlRequest.key())
                                                        .build();

    SdkHttpRequest httpRequest = S3EndpointUtils.applyEndpointConfiguration(marshalledRequest,
                                                                            getObjectRequest,
                                                                            resolvedRegion,
                                                                            s3Configuration,
                                                                            endpointOverridden)
                                                .sdkHttpRequest();

    try {
        return httpRequest.getUri().toURL();
    } catch (MalformedURLException exception) {
        throw SdkException.create("Generated URI is malformed: " + httpRequest.getUri(),
                                  exception);
    }
}
 
示例15
@Test
public void specifiedInOverrideConfig_shouldUse() {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);

    String profileFileContent =
        "[default]\n" +
        "s3_use_arn_region = true\n";

    ProfileFile profileFile = ProfileFile.builder()
                                         .type(ProfileFile.Type.CONFIGURATION)
                                         .content(new StringInputStream(profileFileContent))
                                         .build();

    S3Client s3 = S3Client.builder()
                          .region(Region.US_WEST_2)
                          .credentialsProvider(AnonymousCredentialsProvider.create())
                          .overrideConfiguration(c -> c.defaultProfileFile(profileFile)
                                                       .defaultProfileName("default")
                                                       .addExecutionInterceptor(interceptor)
                                                       .retryPolicy(r -> r.numRetries(0)))
                          .build();

    String arn = "arn:aws:s3:us-banana-46:12345567890:accesspoint:foo";
    assertThatThrownBy(() -> s3.getObject(r -> r.bucket(arn).key("bar"))).isInstanceOf(SdkException.class);

    ArgumentCaptor<Context.BeforeTransmission> context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    Mockito.verify(interceptor).beforeTransmission(context.capture(), any());

    String host = context.getValue().httpRequest().host();
    assertThat(host).contains("us-banana-46");
}
 
示例16
private void clientClockSkewAdjustsWithoutRetries(Runnable call) {
    Instant actualTime = Instant.now();
    Instant skewedTime = actualTime.plus(7, HOURS);

    // Force the client time forward
    stubForResponse(skewedTime, 400, "RequestTimeTooSkewed");
    assertThatThrownBy(call::run).isInstanceOfAny(SdkException.class, CompletionException.class);

    // Verify the next call uses that time
    stubForResponse(actualTime, 200, "");
    call.run();
    assertSigningDateApproximatelyEquals(getRecordedRequests().get(0), skewedTime);
}
 
示例17
@Test
public void legacyRetryModeIsFourAttempts() {
    stubThrottlingResponse();
    ClientT client = clientBuilder().overrideConfiguration(o -> o.retryPolicy(RetryMode.LEGACY)).build();
    assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class);
    verifyRequestCount(4);
}
 
示例18
@Test
public void standardRetryModeIsThreeAttempts() {
    stubThrottlingResponse();
    ClientT client = clientBuilder().overrideConfiguration(o -> o.retryPolicy(RetryMode.STANDARD)).build();
    assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class);
    verifyRequestCount(3);
}
 
示例19
@Test
public void retryModeCanBeSetByProfileFile() {
    ProfileFile profileFile = ProfileFile.builder()
                                         .content(new StringInputStream("[profile foo]\n" +
                                                                        "retry_mode = standard"))
                                         .type(ProfileFile.Type.CONFIGURATION)
                                         .build();
    stubThrottlingResponse();
    ClientT client = clientBuilder().overrideConfiguration(o -> o.defaultProfileFile(profileFile)
                                                                 .defaultProfileName("foo")).build();
    assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class);
    verifyRequestCount(3);
}
 
示例20
/**
 * Standard constructor
 * @param pojoSupplier A method that supplies an empty builder of the correct type
 * @param successResponseTransformer A function that can unmarshall a response object from parsed XML
 * @param errorResponseTransformer A function that can unmarshall an exception object from parsed XML
 * @param decorateContextWithError A function that determines if the response was an error or not
 * @param needsConnectionLeftOpen true if the underlying connection should not be closed once parsed
 */
public AwsXmlPredicatedResponseHandler(
    Function<SdkHttpFullResponse, SdkPojo> pojoSupplier,
    Function<AwsXmlUnmarshallingContext, OutputT> successResponseTransformer,
    Function<AwsXmlUnmarshallingContext, ? extends SdkException> errorResponseTransformer,
    Function<AwsXmlUnmarshallingContext, AwsXmlUnmarshallingContext> decorateContextWithError,
    boolean needsConnectionLeftOpen) {

    this.pojoSupplier = pojoSupplier;
    this.successResponseTransformer = successResponseTransformer;
    this.errorResponseTransformer = errorResponseTransformer;
    this.decorateContextWithError = decorateContextWithError;
    this.needsConnectionLeftOpen = needsConnectionLeftOpen;
}
 
示例21
/**
 * Responsible for handling an error response, including unmarshalling the error response
 * into the most specific exception type possible, and throwing the exception.
 */
private SdkException handleErrorResponse(AwsXmlUnmarshallingContext parsedResponse) {
    try {
        SdkException exception = errorResponseTransformer.apply(parsedResponse);
        exception.fillInStackTrace();
        SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Received error response: " + exception);
        return exception;
    } catch (Exception e) {
        String errorMessage = String.format("Unable to unmarshall error response (%s). " +
                                            "Response Code: %d, Response Text: %s", e.getMessage(),
                                            parsedResponse.sdkHttpFullResponse().statusCode(),
                                            parsedResponse.sdkHttpFullResponse().statusText().orElse("null"));
        throw SdkClientException.builder().message(errorMessage).cause(e).build();
    }
}
 
示例22
/**
 * Error responses are never streaming so we always use {@link AsyncResponseHandler}.
 *
 * @return Async handler for error responses.
 */
private TransformingAsyncResponseHandler<? extends SdkException> resolveErrorResponseHandler(
    HttpResponseHandler<? extends SdkException> errorHandler,
    ExecutionContext executionContext,
    Function<SdkHttpFullResponse, SdkHttpFullResponse> responseAdapter) {
    return new AsyncResponseHandler<>(errorHandler,
                                      responseAdapter,
                                      executionContext.executionAttributes());
}
 
示例23
public Response<OutputT> execute(SdkHttpFullRequest request, RequestExecutionContext context) throws Exception {
    RetryableStageHelper retryableStageHelper = new RetryableStageHelper(request, context, dependencies);

    while (true) {
        retryableStageHelper.startingAttempt();

        if (!retryableStageHelper.retryPolicyAllowsRetry()) {
            throw retryableStageHelper.retryPolicyDisallowedRetryException();
        }

        Duration backoffDelay = retryableStageHelper.getBackoffDelay();
        if (!backoffDelay.isZero()) {
            retryableStageHelper.logBackingOff(backoffDelay);
            TimeUnit.MILLISECONDS.sleep(backoffDelay.toMillis());
        }

        Response<OutputT> response;
        try {
            retryableStageHelper.logSendingRequest();
            response = requestPipeline.execute(retryableStageHelper.requestToSend(), context);
        } catch (SdkException | IOException e) {
            retryableStageHelper.setLastException(e);
            continue;
        }

        retryableStageHelper.setLastResponse(response.httpResponse());

        if (!response.isSuccess()) {
            retryableStageHelper.adjustClockIfClockSkew(response);
            retryableStageHelper.setLastException(response.exception());
            continue;
        }

        retryableStageHelper.attemptSucceeded();
        return response;
    }
}
 
示例24
/**
 * Update the {@link #getLastException()} value for this helper. This will be used to determine whether the request should
 * be retried.
 */
public void setLastException(Throwable lastException) {
    if (lastException instanceof CompletionException) {
        setLastException(lastException.getCause());
    } else if (lastException instanceof SdkException) {
        this.lastException = (SdkException) lastException;
    } else {
        this.lastException = SdkClientException.create("Unable to execute HTTP request: " + lastException.getMessage(),
                                                       lastException);
    }
}
 
示例25
public CombinedResponseAsyncHttpResponseHandler(
    TransformingAsyncResponseHandler<OutputT> successResponseHandler,
    TransformingAsyncResponseHandler<? extends SdkException> errorResponseHandler) {

    this.successResponseHandler = successResponseHandler;
    this.errorResponseHandler = errorResponseHandler;
}
 
示例26
@Override
public CompletableFuture<Response<OutputT>> prepare() {
    this.response.set(null);
    CompletableFuture<OutputT> preparedTransformFuture = successResponseHandler.prepare();

    CompletableFuture<? extends SdkException> preparedErrorTransformFuture = errorResponseHandler == null ? null :
        errorResponseHandler.prepare();

    headersFuture = new CompletableFuture<>();

    return headersFuture.thenCompose(headers -> {
        if (headers.isSuccessful()) {
            return preparedTransformFuture.thenApply(
                r -> Response.<OutputT>builder().response(r)
                                                .httpResponse(response.get())
                                                .isSuccess(true)
                                                .build());
        }

        if (preparedErrorTransformFuture != null) {
            return preparedErrorTransformFuture.thenApply(
                e -> Response.<OutputT>builder().exception(e)
                                                .httpResponse(response.get())
                                                .isSuccess(false)
                                                .build());
        }
        return CompletableFuture.completedFuture(
            Response.<OutputT>builder().httpResponse(response.get())
                                       .isSuccess(false)
                                       .build());
    });
}
 
示例27
/**
 * Wraps the given {@code Throwable} in {@link SdkException} if necessary.
 */
public static SdkException asSdkException(Throwable t) {
    if (t instanceof SdkException) {
        return (SdkException) t;
    }
    return SdkClientException.builder().cause(t).build();
}
 
示例28
@Override
public Integer apply(SdkException e) {
    if (throttlingExceptionCost != null && RetryUtils.isThrottlingException(e)) {
        return throttlingExceptionCost;
    }

    return defaultExceptionCost;
}
 
示例29
public static <T> HttpResponseHandler<Response<T>> combinedSyncResponseHandler(
    HttpResponseHandler<T> successResponseHandler,
    HttpResponseHandler<? extends SdkException> failureResponseHandler) {

    return new CombinedResponseHandler<>(
        successResponseHandler == null ? noOpSyncResponseHandler() : successResponseHandler,
        failureResponseHandler == null ? noOpSyncResponseHandler() : failureResponseHandler);
}
 
示例30
public static <T> TransformingAsyncResponseHandler<Response<T>> combinedAsyncResponseHandler(
    TransformingAsyncResponseHandler<T> successResponseHandler,
    TransformingAsyncResponseHandler<? extends SdkException> failureResponseHandler) {

    return new CombinedResponseAsyncHttpResponseHandler<>(
        successResponseHandler == null ? noOpResponseHandler() : successResponseHandler,
        failureResponseHandler == null ? noOpResponseHandler() : failureResponseHandler);
}