title: How do custom exception in Webflux WebClient?
body: <p>In my webflux app I want to send some requests via <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client" rel="nofollow noreferrer">WebClient</a>. I want to handle  all conditions (<strong>200</strong>, <strong>401</strong>, <strong>403</strong> and <strong>...</strong> response) and then response a json to client. for error status codes, I want to use @RestControllerAdvice, So I have to throw a custom exception and then in controller advice handle custom json. See the sample code:</p>
  
  <pre><code>WebClient.create().post
     .uri("URI")
     .retrieve()
     .onStatus(HttpStatus::is4xxClientError, {
         // create Mono&lt;CustomException&gt; 
     }
     .bodyToMono(ResponseDto.class)
  </code></pre>
  
  <p>And now exception handler is as follow:</p>
  
  <pre><code>@ResponseBody
  @ResponseStatus(...)
  @ExceptionHandler(CustomException1.class)
  public void customException1(CustomException1 exception) {
     //do Something width response body
  }
  
  @ExceptionHandler(CustomException2.class)
  public void customException2(CustomException2 exception) {
     //do Something width response body
  }
  </code></pre>
  
  <p>Webclient get a 401 response woth json body as follow:</p>
  
  <pre><code>{
     "message": "Password is incorrect"
  }
  </code></pre>
  
  <p>I can create <code>Mono.error(new CustomException())</code>, But the problem is WebClient response body. If the message be "Password is incorrect", I want to send client:</p>
  
  <blockquote>
    <p>Username or Password is incorrect</p>
  </blockquote>
  
  <p>How do I do?</p>