I want to add a custom error Zuul Filter and want to make sure SendErrorFilter does not execute. I have looked at few github links including Spring-cloud/spring-cloud-netflix and various stack-overflow questions:-
Overriding Zuul Filter SendErrorFilter
My code is as follows-
public class CustomErrorFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(CustomErrorFilter.class);
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return -1;
}
@Override
public boolean shouldFilter() {
RequestContext ctx=RequestContext.getCurrentContext();
if(ctx.getThrowable()!=null)
return true;
else
return false;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.setThrowable(null); // response is not returned unless
throwable is set to null.
ctx.remove("error.status_code");
ctx.setResponseBody(“Error”);
ctx.getResponse().setContentType("text/plain");
ctx.setResponseStatusCode(400);
}
return null;
}
I am having the following issues-
Setting filter order to
-1
does not prohibitsendErrorFilter
from running.To stop sendErrorFilter from running, I need to set
zuul.SendErrorFilter.error.disable=true
inbootstrap.yml
To get a response body which is set in the custom error filter, i need to set
throwable
tonull
as mentioned in the github.Setting a filter as type "error" does nothing, and the custom filer does not run.
I would like someone to explain, what I am doing wrong and what is the most correct way of handling custom error filters, because there is lot of conflicting information available on the web.
Dependencies-
spring cloud - Edgware.RELEASE
spring cloud netflix starter zuul- 1.4.3.RELEASE