BAEL-6865, implemented review comments
This commit is contained in:
parent
e521083c14
commit
2f309cff86
|
@ -1,27 +0,0 @@
|
|||
package com.baeldung.aggregateEx.entity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExceptionAggregator extends RuntimeException {
|
||||
private List<Throwable> exceptions;
|
||||
|
||||
public ExceptionAggregator(String message) {
|
||||
super(message);
|
||||
exceptions = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Throwable> getExceptions() {
|
||||
return exceptions;
|
||||
}
|
||||
|
||||
public Throwable addException(Throwable e) {
|
||||
this.addSuppressed(e);
|
||||
exceptions.add(e);
|
||||
return e;
|
||||
}
|
||||
|
||||
public void addExceptions(List<Throwable> exceptions) {
|
||||
exceptions.forEach(this::addException);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
package com.baeldung.aggregateEx;
|
||||
|
||||
import com.baeldung.aggregateEx.entity.ExceptionAggregator;
|
||||
package com.baeldung.aggregateexception;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -11,8 +9,6 @@ public class CustomCollector<T, R> {
|
|||
private final List<R> results = new ArrayList<>();
|
||||
private final List<Throwable> exceptions = new ArrayList<>();
|
||||
|
||||
private final ExceptionAggregator exceptionAggregator = new ExceptionAggregator("Exceptions occurred");
|
||||
|
||||
public static <T, R> Collector<T, ?, CustomCollector<T, R>> of(Function<T, R> mapper) {
|
||||
return Collector.of(
|
||||
CustomCollector::new,
|
||||
|
@ -22,13 +18,11 @@ public class CustomCollector<T, R> {
|
|||
collector.results.add(result);
|
||||
} catch (Exception e) {
|
||||
collector.exceptions.add(e);
|
||||
collector.exceptionAggregator.addException(e);
|
||||
}
|
||||
},
|
||||
(left, right) -> {
|
||||
left.results.addAll(right.results);
|
||||
left.exceptions.addAll(right.exceptions);
|
||||
left.exceptionAggregator.addExceptions(right.exceptions);
|
||||
return left;
|
||||
}
|
||||
);
|
||||
|
@ -41,8 +35,4 @@ public class CustomCollector<T, R> {
|
|||
public List<Throwable> getExceptions() {
|
||||
return exceptions;
|
||||
}
|
||||
|
||||
public ExceptionAggregator getExceptionAggregator() {
|
||||
return exceptionAggregator;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.aggregateEx;
|
||||
package com.baeldung.aggregateexception;
|
||||
|
||||
import com.baeldung.aggregateEx.entity.Result;
|
||||
import com.baeldung.aggregateexception.entity.Result;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.aggregateEx.entity;
|
||||
package com.baeldung.aggregateexception.entity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.aggregateEx;
|
||||
package com.baeldung.aggregateexception;
|
||||
|
||||
import com.baeldung.aggregateEx.entity.ExceptionAggregator;
|
||||
import com.baeldung.aggregateEx.entity.Result;
|
||||
import com.baeldung.aggregateexception.entity.Result;
|
||||
import io.vavr.control.Either;
|
||||
import io.vavr.control.Try;
|
||||
import org.junit.Test;
|
||||
|
@ -18,12 +17,12 @@ import static org.junit.Assert.assertEquals;
|
|||
|
||||
|
||||
public class AggregateExceptionHandlerUnitTest {
|
||||
static Logger logger = LoggerFactory.getLogger(AggregateExceptionHandlerUnitTest.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(AggregateExceptionHandlerUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenExtractedMethod_whenFoundEx_thenSuppressExIntoRuntimeEx() {
|
||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||
Throwable runEx = Arrays.stream(strings)
|
||||
RuntimeException runEx = Arrays.stream(strings)
|
||||
.map(str -> callProcessThrowsExAndNoOutput(str))
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(new RuntimeException("Errors Occurred"), (o1, o2) -> {
|
||||
|
@ -58,11 +57,13 @@ public class AggregateExceptionHandlerUnitTest {
|
|||
@Test
|
||||
public void givenProcessMethod_whenStreamResultHasExAndOutput_thenHandleExceptionListAndOutputList() {
|
||||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||
Map map = strings.stream()
|
||||
Map<Boolean, List<Object>> map = strings.stream()
|
||||
.map(s -> processReturnsExAndOutput(s))
|
||||
.collect(Collectors.partitioningBy(o -> o instanceof RuntimeException, Collectors.toList()));
|
||||
assert(map.containsKey(Boolean.TRUE) && map.containsKey(Boolean.FALSE));
|
||||
handleExceptionsAndOutputs((List<RuntimeException>) map.get(Boolean.TRUE), (List<Integer>)map.get(Boolean.FALSE));
|
||||
|
||||
List<Object> exceptions = map.getOrDefault(Boolean.TRUE, List.of());
|
||||
List<Object> results = map.getOrDefault(Boolean.FALSE, List.of());
|
||||
handleExceptionsAndOutputs(exceptions, results);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -70,17 +71,15 @@ public class AggregateExceptionHandlerUnitTest {
|
|||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||
strings.stream()
|
||||
.map(CustomMapper.mapper(Integer::parseInt))
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> handleErrorsAndOutPutForResult(list)));
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> handleErrorsAndOutputForResult(list)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomCollector_whenStreamResultHasExAndSuccess_thenHandleAggrExceptionAndResults() {
|
||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||
Arrays.stream(strings)
|
||||
.collect(Collectors.collectingAndThen(CustomCollector.of(Integer::parseInt), col -> {
|
||||
handleExAndResults(col.getExceptionAggregator(), col.getResults());
|
||||
return col;
|
||||
}));
|
||||
.collect(Collectors.collectingAndThen(CustomCollector.of(Integer::parseInt),
|
||||
col -> handleExAndResults(col.getExceptions(), col.getResults())));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -88,19 +87,17 @@ public class AggregateExceptionHandlerUnitTest {
|
|||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||
strings.stream()
|
||||
.map(str -> Try.of(() -> Integer.parseInt(str)).toEither())
|
||||
.collect(Collectors.collectingAndThen(Collectors.partitioningBy(Either::isLeft, Collectors.toList()), map -> {
|
||||
handleErrorsAndOutPutForEither(map);
|
||||
return map;
|
||||
}));
|
||||
.collect(Collectors.collectingAndThen(Collectors.partitioningBy(Either::isLeft, Collectors.toList())
|
||||
, map -> handleErrorsAndOutputForEither(map)));
|
||||
}
|
||||
|
||||
private static void processThrowsExAndNoOutput(String input) {
|
||||
//return exception when input is "a", "b", "c"
|
||||
//throw exception when input is "a", "b", "c"
|
||||
if (input.matches("[a-c]")) {
|
||||
throw new RuntimeException("Downstream method throws exception for " + input);
|
||||
}
|
||||
}
|
||||
private static Throwable callProcessThrowsExAndNoOutput(String input) {
|
||||
private static RuntimeException callProcessThrowsExAndNoOutput(String input) {
|
||||
try {
|
||||
processThrowsExAndNoOutput(input);
|
||||
return null;
|
||||
|
@ -122,22 +119,24 @@ public class AggregateExceptionHandlerUnitTest {
|
|||
logger.error("Process Exception" + throwable.getMessage());
|
||||
}
|
||||
|
||||
private static void handleExceptionsAndOutputs(List<RuntimeException> exs, List output) {
|
||||
logger.info("handle exceptions and output");
|
||||
private static void handleExceptionsAndOutputs(List<Object> exs, List<Object> output) {
|
||||
logger.info("number of exceptions " + exs.size() + " number of outputs " + output.size());
|
||||
}
|
||||
|
||||
private static void handleExAndResults(ExceptionAggregator exAgg, List<Integer> results ) {
|
||||
logger.info("handle aggregated exceptions and results" + exAgg.getExceptions().size() + " " + results.size());
|
||||
private static String handleExAndResults(List<Throwable> ex, List<Integer> results ) {
|
||||
logger.info("handle aggregated exceptions and results" + ex.size() + " " + results.size());
|
||||
return "Exceptions and Results Handled";
|
||||
}
|
||||
|
||||
private static void handleErrorsAndOutPutForEither(Map<Boolean, List<Either<Throwable, Integer>>> map) {
|
||||
private static String handleErrorsAndOutputForEither(Map<Boolean, List<Either<Throwable, Integer>>> map) {
|
||||
logger.info("handle errors and output");
|
||||
map.get(Boolean.TRUE).forEach(either -> logger.error("Process Exception " + either.getLeft()));
|
||||
map.getOrDefault(Boolean.TRUE, List.of()).forEach(either -> logger.error("Process Exception " + either.getLeft()));
|
||||
|
||||
map.get(Boolean.FALSE).forEach(either -> logger.info("Process Result " + either.get()));
|
||||
map.getOrDefault(Boolean.FALSE, List.of()).forEach(either -> logger.info("Process Result " + either.get()));
|
||||
return "Errors and Output Handled";
|
||||
}
|
||||
|
||||
private static String handleErrorsAndOutPutForResult(List<Result<Integer, Throwable>> successAndErrors) {
|
||||
private static String handleErrorsAndOutputForResult(List<Result<Integer, Throwable>> successAndErrors) {
|
||||
logger.info("handle errors and output");
|
||||
successAndErrors.forEach(result -> {
|
||||
if (result.getException().isPresent()) {
|
Loading…
Reference in New Issue