Commit 95541e01 by Spencer Gibb

only run SendErrorFilter once (fixes recursive forwarding if /error fails)

parent 64e5a2e0
......@@ -14,6 +14,8 @@ import javax.servlet.RequestDispatcher;
@Slf4j
public class SendErrorFilter extends ZuulFilter {
protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";
@Value("${error.path:/error}")
private String errorPath;
......@@ -30,7 +32,8 @@ public class SendErrorFilter extends ZuulFilter {
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
return ctx.containsKey("error.status_code");
//only forward to errorPath if it hasn't been forwarded to already
return ctx.containsKey("error.status_code") && !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false);
}
@Override
......@@ -44,10 +47,11 @@ public class SendErrorFilter extends ZuulFilter {
ctx.getRequest().setAttribute("javax.servlet.error.exception", e);
}
ctx.getRequest().setAttribute("javax.servlet.error.status_code", statusCode);
RequestDispatcher dispatcher = ctx.getRequest().getRequestDispatcher(errorPath);
if (dispatcher != null) {
dispatcher.forward(ctx.getRequest(), ctx.getResponse());
}
RequestDispatcher dispatcher = ctx.getRequest().getRequestDispatcher(errorPath);
if (dispatcher != null) {
ctx.set(SEND_ERROR_FILTER_RAN, true);
dispatcher.forward(ctx.getRequest(), ctx.getResponse());
}
} catch (Exception e) {
Throwables.propagate(e);
}
......
......@@ -48,4 +48,13 @@ public class SendErrorFilterTests {
assertTrue("shouldFilter returned false", filter.shouldFilter());
filter.run();
}
@Test
public void doesNotRunTwice() {
SendErrorFilter filter = createSendErrorFilter(new MockHttpServletRequest());
assertTrue("shouldFilter returned false", filter.shouldFilter());
filter.run();
assertFalse("shouldFilter returned true", filter.shouldFilter());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment