Add example for mapError (#29913)

This commit is contained in:
Nitika Agarwal 2021-01-13 12:35:47 +05:30 committed by GitHub
parent 294661bde1
commit 8361b2a1b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.stream.operators.sourceorflow;
import akka.actor.ActorSystem;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import java.util.Arrays;
public class MapError {
public static void main(String[] args) {
// #map-error
final ActorSystem system = ActorSystem.create("mapError-operator-example");
Source.from(Arrays.asList(-1, 0, 1))
.map(x -> 1 / x)
.mapError(
ArithmeticException.class,
(ArithmeticException e) ->
new UnsupportedOperationException("Divide by Zero Operation is not supported."))
.runWith(Sink.seq(), system)
.whenComplete(
(result, exception) -> {
if (result != null) System.out.println(result.toString());
else System.out.println(exception.getMessage());
});
// prints "Divide by Zero Operation is not supported."
// #map-error
}
}