diff --git a/core/src/main/java/feign/MethodHandler.java b/core/src/main/java/feign/MethodHandler.java index 141e6442..5e6c8885 100644 --- a/core/src/main/java/feign/MethodHandler.java +++ b/core/src/main/java/feign/MethodHandler.java @@ -136,15 +136,16 @@ interface MethodHandler { if (logLevel.get() != Logger.Level.NONE) { response = logger.logAndRebufferResponse(metadata.configKey(), logLevel.get(), response, elapsedTime); } + if (Response.class == metadata.returnType()) { + if (response.body() == null) { + return response; + } + // Ensure the response body is disconnected + byte[] bodyData = Util.toByteArray(response.body().asInputStream()); + return Response.create(response.status(), response.reason(), response.headers(), bodyData); + } if (response.status() >= 200 && response.status() < 300) { - if (Response.class == metadata.returnType()) { - if (response.body() == null) { - return response; - } - // Ensure the response body is disconnected - byte[] bodyData = Util.toByteArray(response.body().asInputStream()); - return Response.create(response.status(), response.reason(), response.headers(), bodyData); - } else if (void.class == metadata.returnType()) { + if (void.class == metadata.returnType()) { return null; } else { return decode(response); diff --git a/core/src/test/java/feign/FeignTest.java b/core/src/test/java/feign/FeignTest.java index 801422e2..9b041186 100644 --- a/core/src/test/java/feign/FeignTest.java +++ b/core/src/test/java/feign/FeignTest.java @@ -536,4 +536,16 @@ public class FeignTest { server.shutdown(); } } + + @Test + public void responseTypeGetsReturnedDirectlyEvenWithErrorResponseCode() throws Exception { + final MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setResponseCode(404)); + server.play(); + + TestInterface testInterface = Feign.builder().options(new Request.Options(1000, 1000)) + .target(TestInterface.class, "http://localhost:" + server.getPort()); + Response response = testInterface.response(); + assertEquals(response.status(), 404); + } }