Created
October 11, 2019 22:38
-
-
Save akhilbojedla/a4cce6a9236c584432ba1ac6393fcefe to your computer and use it in GitHub Desktop.
Handle Errors using onErrorResume on Spring WebFlux Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytecodehq.routes; | |
import reactor.core.publisher.Mono; | |
import java.io.IOException; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class HelloController { | |
/** | |
* Provide a fallback handler function on any exception | |
*/ | |
@GetMapping | |
public Mono<String> helloWorld() { | |
return Mono.<String>error(IOException::new) | |
.onErrorResume(e -> Mono.just("Error : " + e.getMessage())); | |
} | |
/** | |
* Provide a fallback handler function on {@link IOException} | |
*/ | |
@GetMapping("/2") | |
public Mono<String> helloWorld2() { | |
return Mono.<String>error(IOException::new) | |
.onErrorResume(IOException.class, e -> Mono.just("Error : " + e.getMessage())); | |
} | |
/** | |
* Provide a fallback handler function on truthfulness of predicate | |
*/ | |
@GetMapping("/3") | |
public Mono<String> helloWorld3() { | |
return Mono.<String>error(IOException::new) | |
.onErrorResume(e -> e instanceof IOException, e -> Mono.just("Error : " + e.getMessage())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment