title: Webclient Error Handling - Spring Webflux
body: <p>I want to throw my custom exceptions with the following conditions:</p>
  <ol>
  <li><p>If I am getting proper error response in json format, I want to deserialize it and throw my exception named <strong>CommonException</strong> inside <strong>onStatus()</strong></p>
  </li>
  <li><p>If I am receiving an HTML content as part of response or deserialization didnt happen successfully then I want to throw <strong>GenericException</strong> which I am creating inside <strong>onErrorMap()</strong></p>
  </li>
  <li><p>While throwing a GenericException, I want to pass the same HttpStatus code to upstream which I am getting from downstream response.</p>
  <pre><code> IdVerificationResponse idVerificationResponse = client.get()
   .uri(idVerificationUrl)
   .headers(headers -&gt; headers.addAll(httpEntity.getHeaders()))
   .retrieve()
   .onStatus(HttpStatus::isError, response -&gt;
       //Throw this one only if deserialization of error response to IdVerificationErrorResponse class happens successfully
  
       response.bodyToMono(IdVerificationErrorResponse.class)
           .flatMap(error -&gt; Mono.error(CommonException.builder().message(error.getCustomMessage()).build()))
   )
   .bodyToMono(IdVerificationResponse.class)
   .onErrorMap(error -&gt; {
       //Come over here only if there is an error in deserialization in onStatus()
  
       //How to get the HttpStatus we are getting as part of error response from the downstream
       HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
       ApiErrorDetails errorDetailsObj = ApiErrorDetails.builder().errorCode(httpStatus.name()).errorDescription(&quot;Error related to HTML&quot;)
             .errorDetails(&quot;Error related to HTML&quot;).build();
       ErrorDetails errorDetails = ErrorDetails.builder().errors(errorDetailsObj).build();
       return GenericException.builder().errorDetails(errorDetails).httpStatus(httpStatus).build();
   }).block();
  </code></pre>
  </li>
  </ol>
  <p>Currently onErrorMap() is getting called everytime and overriding the exception I am throwing inside onStatus()</p>