title: Spring WebClient - How to handle error scenarios
body: <p>We're using <code>org.springframework.web.reactive.function.client.WebClient</code> with
  <code>reactor.netty.http.client.HttpClient</code> as part of Spring 5.1.9 to make requests using the <code>exchange()</code> method. The <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.RequestHeadersSpec.html#exchange--" rel="noreferrer">documentation</a> for this method highlights the following:</p>
  
  <blockquote>
    <p>... when using exchange(), it is the responsibility of the application
    to consume any response content regardless of the scenario (success,
    error, unexpected data, etc). Not doing so can cause a memory leak.</p>
  </blockquote>
  
  <p>Our use of <code>exchange()</code> is rather basic, but the documentation for error scenarios is unclear to me and I want to be certain that we are correctly releasing resources for all outcomes. In essence, we have a blocking implementation which makes a request and returns the <code>ResponseEntity</code> regardless of the response code:</p>
  
  <pre><code>    try {
          ...
          ClientResponse resp = client.method(method).uri(uri).syncBody(body).exchange().block();
          ResponseEntity&lt;String&gt; entity =  resp.toEntity(String.class).block();
          return entity;
      } catch (Exception e) {
          // log error details, return internal server error
      }
  </code></pre>
  
  <p>If I understand the implementation, <code>exchange()</code> will always give us a response if the request was successfully dispatched, regardless of response code (e.g. 4xx, 5xx). In that scenario, we just need to invoke <code>toEntity()</code> to consume the response. My concern is for error scenarios (e.g. no response, low-level connection errors, etc). Will the above exception handling catch all other scenarios and will any of them have a response that needs to be consumed?</p>
  
  <p>Note: <code>ClientResponse.releaseBody()</code> was only introduced in 5.2</p>