title: Feign Error Decoder with custom JSON response
body: <p>I have an API which will return a failure in form of custom response message, while still sending a Http 200 response. Example:</p>
  <pre><code>Status: 200
  Body: {
     &quot;code&quot;:404,
     &quot;message&quot;:&quot;Data not found&quot;,
     &quot;data&quot;:{},
     &quot;status&quot;:&quot;ERROR&quot;
  }
  </code></pre>
  <p>My current implementeation of the ErrorDecoder:</p>
  <pre><code>public class ApiFeignErrorDecoder implements ErrorDecoder {
  
      private final Gson gson;
  
      public ApiFeignErrorDecoder(Gson gson) {
          this.gson = gson;
      }
  
      @Override
      public Exception decode(String methodKey, Response response) {
  
          try {
              ApiResponse apiResponse = gson.fromJson(response.body().asReader(), BaseApiResponse.class);
              ResponseStatus status = apiResponse.getStatus();
              switch (status) {
                  case SUCCESS:
                      return null;
                  case FAIL:
                      return new Exception(&quot;Failure message recorded&quot;);
                  case ERROR:
                      return new Exception(&quot;Error message recorded&quot;);
                  default:
                      return new Exception(&quot;No suitable status found.&quot;);
              }
          } catch (IOException e) {
              e.printStackTrace();
              return new ErrorDecoder.Default().decode(methodKey, response);
          }
      }
  
  }
  </code></pre>
  <p>The problem I have is Feign/Spring will only switch into the ErrorDecoder if the HttpStatus is &gt; 300.  The JavaDocs from the error decoder also describe this limitation:</p>
  <p>&quot;Error handling Responses where Response.status() is not in the 2xx range are classified as errors,addressed by the ErrorDecoder. That said, certain RPC apis return errors defined in the Response.body() even on a 200 status. For example, in the DynECT api, a job still running condition is returned with a 200 status, encoded in json. When scenarios like this occur, you should raise an application-specific exception (which may be retryable).&quot;</p>
  <p>My question now is what/how exactly can I implement these or is there a way to extend the ErrorDecoder in such a way that I will be able to handle these error messages. I think I should be able to put them into the either the Decoder or even Implement/override the HttpClient but I'm not sure what the correct/best way is.</p>