Return HTTP status code 504 for Hystrix timeout in Zuul proxy (issue 2965) (#3060)

* Fixes gh-2965
This commit is contained in:
Dmitrii Priporov
2018-07-10 15:05:01 -04:00
committed by Ryan Baxter
parent cef5b26425
commit 26331d1a2c
2 changed files with 17 additions and 3 deletions
@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.zuul.filters.post;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import com.netflix.client.ClientException;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
@@ -31,6 +32,8 @@ import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import java.net.SocketTimeoutException;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ERROR_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_ERROR_FILTER_ORDER;
@@ -100,12 +103,23 @@ public class SendErrorFilter extends ZuulFilter {
protected ExceptionHolder findZuulException(Throwable throwable) {
if (throwable.getCause() instanceof ZuulRuntimeException) {
Throwable cause = null;
if (throwable.getCause().getCause() != null) {
cause = throwable.getCause().getCause().getCause();
}
if (cause instanceof ClientException && cause.getCause() != null
&& cause.getCause().getCause() instanceof SocketTimeoutException) {
ZuulException zuulException = new ZuulException("", 504,
ZuulException.class.getName() + ": Hystrix Readed time out");
return new ZuulExceptionHolder(zuulException);
}
// this was a failure initiated by one of the local filters
if(throwable.getCause().getCause() instanceof ZuulException) {
return new ZuulExceptionHolder((ZuulException) throwable.getCause().getCause());
}
}
if (throwable.getCause() instanceof ZuulException) {
// wrapped zuul exception
return new ZuulExceptionHolder((ZuulException) throwable.getCause());
@@ -124,7 +124,7 @@ public abstract class RibbonRetryIntegrationTestBase {
"http://localhost:" + this.port + uri, HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
LOG.info("Response Body: " + result.getBody());
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
assertEquals(HttpStatus.GATEWAY_TIMEOUT, result.getStatusCode());
}
@Test
@@ -134,7 +134,7 @@ public abstract class RibbonRetryIntegrationTestBase {
"http://localhost:" + this.port + uri, HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
LOG.info("Response Body: " + result.getBody());
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
assertEquals(HttpStatus.GATEWAY_TIMEOUT, result.getStatusCode());
}
// Don't use @SpringBootApplication because we don't want to component scan