Example usage for com.amazonaws AmazonServiceException getErrorCode

List of usage examples for com.amazonaws AmazonServiceException getErrorCode

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException getErrorCode.

Prototype

public String getErrorCode() 

Source Link

Document

Returns the AWS error code represented by this exception.

Usage

From source file:org.finra.dm.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses the DescribeSubnets API.
 *//*w  w  w.j a  v a 2  s  .c o m*/
@Override
public List<Subnet> getSubnets(Collection<String> subnetIds, AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
    describeSubnetsRequest.setSubnetIds(subnetIds);
    try {
        DescribeSubnetsResult describeSubnetsResult = ec2Operations.describeSubnets(ec2Client,
                describeSubnetsRequest);
        return describeSubnetsResult.getSubnets();
    } catch (AmazonServiceException amazonServiceException) {
        /*
         * AWS throws a 400 error when any one of the specified subnet ID is not found.
         * We want to catch it and throw as an handled DM error as a 404 not found.
         */
        if (ERROR_CODE_SUBNET_ID_NOT_FOUND.equals(amazonServiceException.getErrorCode())) {
            throw new ObjectNotFoundException(amazonServiceException.getErrorMessage(), amazonServiceException);
        }
        // Any other type of error we throw as is because they are unexpected.
        else {
            throw amazonServiceException;
        }
    }
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try {//from   ww  w  . j a va2s .  com
        return s3Operations.getS3Object(getObjectRequest, s3);
    } catch (AmazonServiceException amazonServiceException) {
        String errorCode = amazonServiceException.getErrorCode();

        switch (errorCode) {
        case S3Operations.ERROR_CODE_ACCESS_DENIED:
            throw new ObjectNotFoundException(
                    "Application does not have access to the specified S3 object at bucket '"
                            + getObjectRequest.getBucketName() + "' and key '" + getObjectRequest.getKey()
                            + "'.",
                    amazonServiceException);
        case S3Operations.ERROR_CODE_NO_SUCH_BUCKET:
            throw new ObjectNotFoundException(
                    "Specified S3 bucket '" + getObjectRequest.getBucketName() + "' does not exist.",
                    amazonServiceException);
        case S3Operations.ERROR_CODE_NO_SUCH_KEY:
            throw new ObjectNotFoundException(
                    "Specified S3 object key '" + getObjectRequest.getKey() + "' does not exist.",
                    amazonServiceException);
        default:
            throw amazonServiceException;
        }
    }
}

From source file:org.finra.herd.dao.helper.AwsExceptionRetryAdvice.java

License:Apache License

private boolean isRetryableException(AmazonServiceException ase) {
    List<String> errorCodesToRetry = herdStringHelper.splitStringWithDefaultDelimiter(getExceptionErrorCodes());

    return errorCodesToRetry.contains(ase.getErrorCode());
}

From source file:org.finra.herd.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses the DescribeSubnets API.
 *///w w w . jav a  2 s  .  c  om
@Override
public List<Subnet> getSubnets(Collection<String> subnetIds, AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
    describeSubnetsRequest.setSubnetIds(subnetIds);
    try {
        DescribeSubnetsResult describeSubnetsResult = ec2Operations.describeSubnets(ec2Client,
                describeSubnetsRequest);
        return describeSubnetsResult.getSubnets();
    } catch (AmazonServiceException amazonServiceException) {
        /*
         * AWS throws a 400 error when any one of the specified subnet ID is not found.
         * We want to catch it and throw as an handled herd error as a 404 not found.
         */
        if (ERROR_CODE_SUBNET_ID_NOT_FOUND.equals(amazonServiceException.getErrorCode())) {
            throw new ObjectNotFoundException(amazonServiceException.getErrorMessage(), amazonServiceException);
        }
        // Any other type of error we throw as is because they are unexpected.
        else {
            throw amazonServiceException;
        }
    }
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * Retrieves an S3 object./*from w  w w . j  a  va2  s  . co  m*/
 *
 * @param s3Client the S3 client
 * @param bucketName the S3 bucket name
 * @param key the S3 object key
 * @param errorOnNoSuchKey true to throw an error when the object key is not found, otherwise return null
 *
 * @return the S3 object
 * @throws ObjectNotFoundException when specified bucket or key does not exist or access to bucket or key is denied
 */
private S3Object getS3Object(AmazonS3Client s3Client, String bucketName, String key, boolean errorOnNoSuchKey) {
    try {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        return s3Operations.getS3Object(getObjectRequest, s3Client);
    } catch (AmazonServiceException amazonServiceException) {
        String errorCode = amazonServiceException.getErrorCode();

        if (S3Operations.ERROR_CODE_ACCESS_DENIED.equals(errorCode)) {
            throw new ObjectNotFoundException(
                    "Application does not have access to the specified S3 object at bucket '" + bucketName
                            + "' and key '" + key + "'.",
                    amazonServiceException);
        } else if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(errorCode)) {
            throw new ObjectNotFoundException("Specified S3 bucket '" + bucketName + "' does not exist.",
                    amazonServiceException);
        } else if (S3Operations.ERROR_CODE_NO_SUCH_KEY.equals(errorCode)) {
            if (errorOnNoSuchKey) {
                throw new ObjectNotFoundException("Specified S3 object key '" + key + "' does not exist.",
                        amazonServiceException);
            } else {
                return null;
            }
        } else {
            throw amazonServiceException;
        }
    }
}

From source file:org.freeeed.aws.SimpleQueueServiceSample.java

License:Apache License

public static void main(String[] args) throws Exception {

    /*//  w ww.j  av  a2  s.  c o m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    try {
        // Create a queue
        System.out.println("Creating a new SQS queue called MyQueue.\n");
        CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue");
        String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        // List queues
        System.out.println("Listing all queues in your account.\n");
        // TODO restore
        System.out.println();

        // Send a message
        System.out.println("Sending a message to MyQueue.\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."));

        // Receive messages
        System.out.println("Receiving messages from MyQueue.\n");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        for (Message message : messages) {
            System.out.println(message.toString());
        }
        System.out.println();

        // Delete a message
        System.out.println("Deleting a message.\n");
        String messageReceiptHandle = messages.get(0).getReceiptHandle();
        sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle));

        // Delete a queue
        System.out.println("Deleting the test queue.\n");
        sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SQS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with SQS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:org.gradle.internal.resource.transport.aws.s3.S3Client.java

License:Apache License

private S3Object doGetS3Object(URI uri, boolean isLightWeight) {
    S3RegionalResource s3RegionalResource = new S3RegionalResource(uri);
    String bucketName = s3RegionalResource.getBucketName();
    String s3BucketKey = s3RegionalResource.getKey();
    configureClient(s3RegionalResource);

    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, s3BucketKey);
    if (isLightWeight) {
        //Skip content download
        getObjectRequest.setRange(0, 0);
    }/*from w ww. j  ava 2s .c o  m*/

    try {
        return amazonS3Client.getObject(getObjectRequest);
    } catch (AmazonServiceException e) {
        String errorCode = e.getErrorCode();
        if (null != errorCode && errorCode.equalsIgnoreCase("NoSuchKey")) {
            return null;
        }
        throw ResourceExceptions.getFailed(uri, e);
    }
}

From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java

License:Apache License

private Map<String, AttributeValue> getInternal(String tableName, Key key, int attempt) {
    GetItemRequest request = new GetItemRequest(tableName, key);
    try {// w ww .ja  va  2s .  co  m
        GetItemResult result = ddb.getItem(request);
        Map<String, AttributeValue> attributes = result.getItem();
        if (attributes == null || attributes.isEmpty()) {
            return null;
        }
        return attributes;
    } catch (AmazonServiceException e) {
        if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such table: " + tableName, e);
        } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
            //retry after a small pause
            DynamoDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            return getInternal(tableName, key, attempt);
        } else {
            throw new DataStoreOperationException("problem with table: " + tableName + ", key: " + key, e);
        }
    }
}

From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java

License:Apache License

private Map<String, AttributeValue> getConsistentInternal(String tableName, Key key, int attempt) {
    GetItemRequest request = new GetItemRequest(tableName, key);
    request.setConsistentRead(true);//from  ww  w. java  2s. com
    try {
        GetItemResult result = ddb.getItem(request);
        Map<String, AttributeValue> attributes = result.getItem();
        if (attributes == null || attributes.isEmpty()) {
            return null;
        }
        return attributes;
    } catch (AmazonServiceException e) {
        if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such table: " + tableName, e);
        } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
            //retry after a small pause
            DynamoDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            return getConsistentInternal(tableName, key, attempt);
        } else {
            throw new DataStoreOperationException("problem with table: " + tableName + ", key: " + key, e);
        }
    }
}

From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java

License:Apache License

private void putItemInternal(String tableName, Map<String, AttributeValue> attributes, int attempt)
        throws DataAccessException {
    try {/*  ww  w. jav a2s  .  co  m*/
        PutItemRequest request = new PutItemRequest(tableName, attributes);
        ddb.putItem(request);
    } catch (AmazonServiceException e) {
        if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such table: " + tableName, e);
        } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
            //retry after a small pause
            DynamoDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            putItemInternal(tableName, attributes, attempt);
        } else {
            throw new DataStoreOperationException(
                    "problem with table: " + tableName + ", attributes: " + attributes, e);
        }
    }
}