8000 Step 5: Error handling complete · blackbeltcoder/reactive-spring@18a795e · GitHub
[go: up one dir, main page]

Skip to content

Commit 18a795e

Browse files
committed
Step 5: Error handling complete
1 parent f2d96ab commit 18a795e

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

reactor/src/test/java/workshop/Step5ErrorHandling.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public void monoShouldEmitAFallbackValueOnError() {
3434

3535
String recoveryValue = "Recovered!";
3636

37-
// TODO: Replace this line with a fallback value in an error happens
38-
Mono<String> withErrorHandling = failedMono;
37+
Mono<String> withErrorHandling = failedMono.onErrorReturn(recoveryValue);
3938

4039
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
4140
}
@@ -47,8 +46,7 @@ public void monoShouldEmitADeferredFallbackValueOnError() {
4746

4847
Mono<String> recoveryValue = Mono.just("Recovered!");
4948

50-
// TODO: Replace this line with a fallback that is computed in a deferred way
51-
Mono<String> withErrorHandling = failedMono;
49+
Mono<String> withErrorHandling = failedMono.onErrorResume(t -> recoveryValue);
5250

5351
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
5452
}
@@ -60,8 +58,7 @@ public void monoShouldEmitAFallbackValueIfEmpty() {
6058

6159
Mono<String> recoveryValue = Mono.just("Recovered!");
6260

63-
// TODO: Replace this line with a fallback that is computed in a deferred way
64-
Mono<String> withErrorHandling = emptyMono;
61+
Mono<String> withErrorHandling = emptyMono.switchIfEmpty(recoveryValue);
6562

6663
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
6764
}
@@ -71,8 +68,7 @@ public void monoShouldTranslateException() {
7168

7269
Mono<String> failedMono = Mono.error(new IllegalStateException("Something bad happened!"));
7370

74-
// TODO: Replace this line with a Mono translating IllegalStateException to MyBusinessException
75-
Mono<String> withErrorHandling = failedMono;
71+
Mono<String> withErrorHandling = failedMono.onErrorMap(MyBusinessException::new);
7672

7773
StepVerifier.create(withErrorHandling).expectError(MyBusinessException.class).verify();
7874
}

0 commit comments

Comments
 (0)
0