Netflix Feign - Propagate Status and Exception through Microservices
<p>I'm using <a href="https://github.com/OpenFeign/feign/blob/master/okhttp/src/main/java/feign/okhttp/OkHttpClient.java" rel="nofollow noreferrer">Netflix Feign</a> to call to one operation of a Microservice A to other other operation of a Microservice B which validates a code using Spring Boot.</p>
<p>The operation of Microservice B throws an exception in case of the validation has been bad. Then I handled in the Microservices and return a <code>HttpStatus.UNPROCESSABLE_ENTITY</code> (422) like next:</p>
<pre><code>@ExceptionHandler({
       ValidateException.class
    })
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    @ResponseBody
    public Object validationException(final HttpServletRequest request, final validateException exception) {
        log.error(exception.getMessage(), exception);
        error.setErrorMessage(exception.getMessage());
        error.setErrorCode(exception.getCode().toString());
        return error;
    }
</code></pre>
<p>So, when Microservice A calls to B in a interface as next:</p>
<pre><code>@Headers(&quot;Content-Type: &quot; + MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestLine(&quot;GET /other&quot;)
void otherOperation(@Param(&quot;other&quot;)  String other );

@Headers(&quot;Content-Type: &quot; + MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestLine(&quot;GET /code/validate&quot;)
Boolean validate(@Param(&quot;prefix&quot;) String prefix);

static PromotionClient connect() {

    return Feign.builder()
        .encoder(new GsonEncoder())
        .decoder(new GsonDecoder())
        .target(PromotionClient.class, Urls.SERVICE_URL.toString());
}
</code></pre>
<p>and the validations fails it returns a internal error 500 with next message:</p>
<pre><code>{
  &quot;timestamp&quot;: &quot;2016-08-05T09:17:49.939+0000&quot;,
  &quot;status&quot;: 500,
  &quot;error&quot;: &quot;Internal Server Error&quot;,
  &quot;exception&quot;: &quot;feign.FeignException&quot;,
  &quot;message&quot;: &quot;status 422 reading Client#validate(String); content:\n{\r\n  \&quot;errorCode\&quot; : \&quot;VALIDATION_EXISTS\&quot;,\r\n  \&quot;errorMessage\&quot; : \&quot;Code already exists.\&quot;\r\n}&quot;,
  &quot;path&quot;: &quot;/code/validate&quot;
}
</code></pre>
<p>But I need to return the same as the Microservice operation B.</p>
<p>Which would be the best ways or techniques to propagate Status and Exceptions through microservices using Netflix Feign?</p>