1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.socialsignin.spring.data.dynamodb.utils;
17
18 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
19 import org.springframework.dao.DataAccessException;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Queue;
26 import java.util.stream.Collectors;
27
28 public interface ExceptionHandler {
29
30 default <T extends DataAccessException> T repackageToException(List<DynamoDBMapper.FailedBatch> failedBatches,
31 Class<T> targetType) {
32
33 Queue<Exception> allExceptions = failedBatches.stream().map(it -> it.getException())
34 .collect(Collectors.toCollection(LinkedList::new));
35
36
37 Exception cause = allExceptions.poll();
38 try {
39 Constructor<T> ctor = targetType.getConstructor(String.class, Throwable.class);
40 T e = ctor.newInstance("Processing of entities failed!", cause);
41
42 allExceptions.stream().forEach(e::addSuppressed);
43 return e;
44 } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
45 | InvocationTargetException e) {
46 assert false;
47 throw new RuntimeException("Could not repackage '" + failedBatches + "' to " + targetType, e);
48 }
49 }
50 }