title: Kotlin Spring Reactive Webflux - Handle WebClient Error
body: <p>I am having troubles trying to handle different errors from calling Spring webflux's web client.</p>
  
  <p>Below is my current code.</p>
  
  <pre><code>    return request
              .bodyToMono(InputMessage::class.java)
              .flatMap { inputMessage -&gt;
                  client
                          .get()
                          .uri { builder -&gt;
                              builder.path("/message")
                                      .queryParam("message", inputMessage.message)
                                      .build()
                          }
                          .retrieve()
                          .onStatus({t: HttpStatus -&gt; t.is5xxServerError}, {c: ClientResponse -&gt; Mono.error(Throwable("Internal Server Error - try again later"))})
                          .bodyToMono(ListOfAddresses::class.java)
              }
              .flatMap { s -&gt; ServerResponse.ok().syncBody(s) }
  </code></pre>
  
  <p>If it errors, it is still returning the full error message from the client's call. </p>
  
  <p>I tried something else, like this</p>
  
  <pre><code>    return request
              .bodyToMono(InputMessage::class.java)
              .flatMap { inputMessage -&gt;
                  client
                          .get()
                          .uri { builder -&gt;
                              builder.path("/message")
                                      .queryParam("message", inputMessage.message)
                                      .build()
                          }
                          .retrieve()
                          .onStatus({t: HttpStatus -&gt; t.is5xxServerError}, {c: ClientResponse -&gt; Mono.error(Throwable("Internal Server Error - try again later"))})
                          .bodyToMono(ListOfAddresses::class.java)
              }
              .flatMap { s -&gt; ServerResponse.ok().syncBody(s) }
              .onErrorResume { e -&gt; Mono.just("Error " + e.message)
                      .flatMap { s -&gt; ServerResponse.ok().syncBody(s) } }
  </code></pre>
  
  <p>It actually works but then I want to handle different Http status codes error (different messages for each Http status code).</p>
  
  <p>How can I modify my code so it will return the custom message I build?</p>