Open
Description
In our consumption of feign we ran into an issue where we need access to the response status code and response body associated to a client error. We have implemented the following ErrorDecoder implementation in our code base and wanted to share it with the community. It seems like others that are familiar with spring RestOperations are accustom to HttpClientErrorException and HttpServerErrorException exception trees versus the stripped down FeignException. The only concern with the implementation below is how to handle the response body if an exception where to happen during the copying process from feign response to a given instance of spring HttpStatusCodeException:
public class SpringWebClientErrorDecoder implements ErrorDecoder {
private ErrorDecoder delegate = new ErrorDecoder.Default();
@Override
public Exception decode(String methodKey, Response response) {
HttpHeaders responseHeaders = new HttpHeaders();
response.headers().entrySet().stream()
.forEach(entry -> responseHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue())));
HttpStatus statusCode = HttpStatus.valueOf(response.status());
String statusText = response.reason();
byte[] responseBody;
try {
responseBody = IOUtils.toByteArray(response.body().asInputStream());
} catch (IOException e) {
throw new RuntimeException("Failed to process response body.", e);
}
if (response.status() >= 400 && response.status() <= 499) {
return new HttpClientErrorException(statusCode, statusText, responseHeaders, responseBody, null);
}
if (response.status() >= 500 && response.status() <= 599) {
return new HttpServerErrorException(statusCode, statusText, responseHeaders, responseBody, null);
}
return delegate.decode(methodKey, response);
}
}