Ignored output of DynamoDBMapper operations Low

Check the return value of batch operations to make sure they succeed. Batch operations can succeed without throwing an exception even if some items in the request fail.

Detector ID
java/aws-dynamodb-mapper-batch-output-ignored@v1.0
Category

Noncompliant example

1@Slf4j
2@Repository
3public class AwsDynamodbMapperBatchOutputIgnoredNoncompliant extends DynamoBatchWriteOutputNoncompliant {
4    //aws-dynamodb-mapper-batch-output-ignored@v1.0
5    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
6    DynamoDBMapper myDynamoDBMapper = new DynamoDBMapper(client);
7    @Override
8    public void mapperNoncompliant(DynamoDBMapperCollection<String> batch) {
9        // Noncompliant: does not have checks to handle errors returned by batch operation.
10        List<FailedBatch> failures = myDynamoDBMapper.batchSave(batch);
11        System.out.println("Completed Dynamo Batch Write Operation");
12        batch.clear();
13    }
14}

Compliant example

1@Slf4j
2@Repository
3public class AwsDynamodbMapperBatchOutputIgnoredCompliant extends DynamoBatchWriteOutputCompliant {
4    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
5    DynamoDBMapper myDynamoDBMapper = new DynamoDBMapper(client);
6    @Override
7    public List<String> mapperCompliant(DynamoDBMapperCollection<String> batch) {
8        // Compliant: has checks to handle errors returned by batch operation.
9        List<FailedBatch> failures = myDynamoDBMapper.batchSave(batch);
10        return failures.stream()
11                .map(FailedBatch -> String.format("messageId:%s failedReason:%s",
12                        FailedBatch.getException(),
13                        FailedBatch.getUnprocessedItems())).collect(Collectors.toList());
14    }
15}