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.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java

License:Apache License

private void putItemVersionedInternal(String tableName, Key key, Map<String, AttributeValue> attributes,
        String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException {
    PutItemRequest request = new PutItemRequest(tableName, attributes)
            .withExpected(getOptimisticVersionCondition(expectedVersion));
    try {/*from  ww w  .j a  v  a  2 s  . c o  m*/
        ddb.putItem(request);
    } catch (AmazonServiceException e) {
        if (DynamoDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) {
            throw new OptimisticLockingException(persistentEntity, key);
        } else 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++;
            putItemVersionedInternal(tableName, key, attributes, expectedVersion, persistentEntity, attempt);
        } else {
            throw new DataStoreOperationException(
                    "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e);
        }
    }
}

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

License:Apache License

private void updateItemInternal(String tableName, Key key, Map<String, AttributeValueUpdate> attributes,
        int attempt) throws DataAccessException {
    try {/*ww w  .  j  av  a2  s  . c  o  m*/
        UpdateItemRequest request = new UpdateItemRequest(tableName, key, attributes);
        ddb.updateItem(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++;
            updateItemInternal(tableName, key, attributes, attempt);
        } else {
            throw new DataStoreOperationException(
                    "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e);
        }
    }
}

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

License:Apache License

private void updateItemVersionedInternal(String tableName, Key key,
        Map<String, AttributeValueUpdate> attributes, String expectedVersion, PersistentEntity persistentEntity,
        int attempt) throws DataAccessException {
    UpdateItemRequest request = new UpdateItemRequest(tableName, key, attributes)
            .withExpected(getOptimisticVersionCondition(expectedVersion));
    try {//w  w  w  .ja v  a  2 s.  c  o m
        ddb.updateItem(request);
    } catch (AmazonServiceException e) {
        if (DynamoDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) {
            throw new OptimisticLockingException(persistentEntity, key);
        } else 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++;
            updateItemVersionedInternal(tableName, key, attributes, expectedVersion, persistentEntity, attempt);
        } else {
            throw new DataStoreOperationException(
                    "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e);
        }
    }
}

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

License:Apache License

private void deleteItemInternal(String tableName, Key key, int attempt) {
    DeleteItemRequest request = new DeleteItemRequest(tableName, key);
    try {//from   ww w.j  a  v a2  s  . c  o m
        ddb.deleteItem(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++;
            deleteItemInternal(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 List<Map<String, AttributeValue>> scanInternal(String tableName, Map<String, Condition> filter, int max,
        int attempt) {
    LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>();
    try {/* w w  w. j  a  v  a2s  .  co  m*/
        ScanRequest request = new ScanRequest(tableName).withScanFilter(filter);
        ScanResult result = ddb.scan(request);
        items.addAll(result.getItems());

        //keep repeating until we get through all matched items
        Key lastKeyEvaluated = null;
        do {
            lastKeyEvaluated = result.getLastEvaluatedKey();
            if (lastKeyEvaluated != null) {
                request = new ScanRequest(tableName).withScanFilter(filter)
                        .withExclusiveStartKey(lastKeyEvaluated);
                result = ddb.scan(request);
                items.addAll(result.getItems());
            }
        } while (lastKeyEvaluated != null && items.size() < max);

        //truncate if needed
        while (items.size() > max) {
            items.removeLast();
        }

        return items;
    } 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 scanInternal(tableName, filter, max, attempt);
        } else {
            throw new DataStoreOperationException("problem with table: " + tableName + ", filter: " + filter,
                    e);
        }
    }
}

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

License:Apache License

private int scanCountInternal(String tableName, Map<String, Condition> filter, int attempt) {
    LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>();
    try {/*  w ww  .  j av a 2  s. c  o  m*/
        ScanRequest request = new ScanRequest(tableName).withScanFilter(filter).withCount(true);
        ScanResult result = ddb.scan(request);
        int count = 0;
        count = count + result.getCount();

        //keep repeating until we get through all matched items
        Key lastKeyEvaluated = null;
        do {
            lastKeyEvaluated = result.getLastEvaluatedKey();
            if (lastKeyEvaluated != null) {
                request = new ScanRequest(tableName).withScanFilter(filter)
                        .withExclusiveStartKey(lastKeyEvaluated).withCount(true);
                result = ddb.scan(request);
                count = count + result.getCount();
            }
        } while (lastKeyEvaluated != null);

        return count;
    } 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 scanCountInternal(tableName, filter, attempt);
        } else {
            throw new DataStoreOperationException("problem with table: " + tableName + ", filter: " + filter,
                    e);
        }
    }
}

From source file:org.grails.datastore.mapping.simpledb.util.SimpleDBTemplateImpl.java

License:Apache License

private Item getInternal(String domainName, String id, int attempt) {
    GetAttributesRequest request = new GetAttributesRequest(domainName, id);
    try {//from w  w  w .j a  v a  2  s  . c o  m
        List<Attribute> attributes = sdb.getAttributes(request).getAttributes();
        if (attributes.isEmpty()) {
            return null;
        }

        return new Item(id, attributes);
    } catch (AmazonServiceException e) {
        if (SimpleDBUtil.AWS_ERR_CODE_NO_SUCH_DOMAIN.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such domain: " + domainName, e);
        } else if (SimpleDBUtil.AWS_ERR_CODE_SERVICE_UNAVAILABLE.equals(e.getErrorCode())) {
            //retry after a small pause
            SimpleDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            return getInternal(domainName, id, attempt);
        } else {
            throw e;
        }
    }
}

From source file:org.grails.datastore.mapping.simpledb.util.SimpleDBTemplateImpl.java

License:Apache License

private Item getConsistentInternal(String domainName, String id, int attempt) {
    //        String selectExpression = "select * from `" + domainName + "` where id = '"+id+"'"; //todo

    //todo - handle exceptions and retries

    GetAttributesRequest request = new GetAttributesRequest(domainName, id);
    request.setConsistentRead(true);/*ww  w. j a va  2  s  .c om*/
    try {
        List<Attribute> attributes = sdb.getAttributes(request).getAttributes();
        if (attributes.isEmpty()) {
            return null;
        }

        return new Item(id, attributes);
    } catch (AmazonServiceException e) {
        if (SimpleDBUtil.AWS_ERR_CODE_NO_SUCH_DOMAIN.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such domain: " + domainName, e);
        } else if (SimpleDBUtil.AWS_ERR_CODE_SERVICE_UNAVAILABLE.equals(e.getErrorCode())) {
            //retry after a small pause
            SimpleDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            return getConsistentInternal(domainName, id, attempt);
        } else {
            throw e;
        }
    }
}

From source file:org.grails.datastore.mapping.simpledb.util.SimpleDBTemplateImpl.java

License:Apache License

private void putAttributesInternal(String domainName, String id, List<ReplaceableAttribute> attributes,
        int attempt) throws DataAccessException {
    try {/* w  w  w  .  j  a  va2s . c o  m*/
        PutAttributesRequest request = new PutAttributesRequest(domainName, id, attributes);
        sdb.putAttributes(request);
    } catch (AmazonServiceException e) {
        if (SimpleDBUtil.AWS_ERR_CODE_NO_SUCH_DOMAIN.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such domain: " + domainName, e);
        } else if (SimpleDBUtil.AWS_ERR_CODE_SERVICE_UNAVAILABLE.equals(e.getErrorCode())) {
            //retry after a small pause
            SimpleDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            putAttributesInternal(domainName, id, attributes, attempt);
        } else {
            throw e;
        }
    }
}

From source file:org.grails.datastore.mapping.simpledb.util.SimpleDBTemplateImpl.java

License:Apache License

private void putAttributesVersionedInternal(String domainName, String id, List<ReplaceableAttribute> attributes,
        String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException {
    PutAttributesRequest request = new PutAttributesRequest(domainName, id, attributes,
            getOptimisticVersionCondition(expectedVersion));
    try {/* w w w .  java  2  s  .  co  m*/
        sdb.putAttributes(request);
    } catch (AmazonServiceException e) {
        if (SimpleDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) {
            throw new OptimisticLockingException(persistentEntity, id);
        } else if (SimpleDBUtil.AWS_ERR_CODE_NO_SUCH_DOMAIN.equals(e.getErrorCode())) {
            throw new IllegalArgumentException("no such domain: " + domainName, e);
        } else if (SimpleDBUtil.AWS_ERR_CODE_SERVICE_UNAVAILABLE.equals(e.getErrorCode())) {
            //retry after a small pause
            SimpleDBUtil.sleepBeforeRetry(attempt);
            attempt++;
            putAttributesVersionedInternal(domainName, id, attributes, expectedVersion, persistentEntity,
                    attempt);
        } else {
            throw e;
        }
    }
}