Skip to content

Instantly share code, notes, and snippets.

@akhilbojedla
Last active October 11, 2019 22:34
Show Gist options
  • Save akhilbojedla/583a9ecaf16532aaa6cff56cbb940410 to your computer and use it in GitHub Desktop.
Save akhilbojedla/583a9ecaf16532aaa6cff56cbb940410 to your computer and use it in GitHub Desktop.
HelloController based on Spring WebFlux handling error with onErrorReturn
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 {
/**
* Returns a static message on all exceptions.
*/
@GetMapping
public Mono<String> helloWorld() {
return Mono.<String>error(IOException::new)
.onErrorReturn("Error");
}
/**
* Returns a static message on {@link IOException}
*/
@GetMapping("/2")
public Mono<String> helloWorld2() {
return Mono.<String>error(IOException::new)
.onErrorReturn(IOException.class, "Error");
}
/**
* Returns a static message based on truthfulness of predicate
*/
@GetMapping("/3")
public Mono<String> helloWorld3() {
return Mono.<String>error(IOException::new)
.onErrorReturn(e -> e instanceof IOException, "Error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment