Search code examples
httpresponsenetflix-zuul

Zuul error filter: set response status dependent on error


I am using Netflix Zuul v2.0 as an authentication proxy, i.e. I am using a "pre" filter to add a JWT token to the request header.

The problem that I have is the following: when the request resource is not available an "Internal Server Error" (500) is returned instead of a "Request Timeout" (408) error. I have solved this by implementing a custom error filter and returning a 408 error whenever an error occurs.

However, now I am returning a "Request Timeout" (408) error for every error, even when the cause was different. e.g. when there was a "bad request" or an "unauthorized" request.

The question is: how do I know inside the error filter what the real cause of the problem was? e.g. I only want to return 408 when there really was a time out. Afaik the only information that I have is that there was an error, hence ctx.getResponseStatusCode() equals 500. Or am I missing anything?

Here is the implementation of the custom error filter:

@Component
public class ConnectionErrorFilter extends ZuulFilter {

  protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";

  @Override
  public String filterType() {
    return FilterConstants.ERROR_TYPE;
  }

  @Override
  public int filterOrder() {
    return FilterConstants.SEND_ERROR_FILTER_ORDER - 1; // Invoke before default error filter
  }

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.set(SEND_ERROR_FILTER_RAN); 
    ctx.setResponseStatusCode(408);  // Workaround because it is here IMO not detectable what the real cause was.
    // Thus, we assume that the cloud was not reachable.
    return null;
  }
}

Solution

  • Upgrading to Zuul 2.0.1.RELEASE resolved the problem. When the request resource is unavailable a Gateway timeout (504) error is returned.