5

How does one use a Mono.error(<Throwable>) but attach information from the body returned from a request?

  • Is there a reactive object that extends Throwable that takes a Mono/Flux object, so the error being thrown will wait for the body to be accounted for?
  • Or is there a way to add some sort of 'flag' onto an existing Mono object to make it fail instantly (to circumvent the requirement to be Throwable)

Example scenario below:

import org.springframework.web.reactive.function.client.WebClient;

private someMethod() {
    webClient.get().retrieve().onStatus(HttpStatus::isError, this::errorHandler)
}

private Mono<? extends Throwable> errorHandler(ClientResponse cr) {
    Mono<String> body = cr.body(BodyExtractors.toMono(String.class));

        ...<do something here>...

    return Mono.error(new WhateverException());
}

Thanks

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
Flavouski
  • 153
  • 1
  • 11

1 Answers1

3
return body.flatMap(str -> Mono.error(new WhateverException(str)));
Flavouski
  • 153
  • 1
  • 11