Skip to content

Instantly share code, notes, and snippets.

@akhilbojedla
Created October 11, 2019 22:38
Show Gist options
  • Save akhilbojedla/a4cce6a9236c584432ba1ac6393fcefe to your computer and use it in GitHub Desktop.
Save akhilbojedla/a4cce6a9236c584432ba1ac6393fcefe to your computer and use it in GitHub Desktop.
Handle Errors using onErrorResume on Spring WebFlux Application
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