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