Customizing Zuul Exception

后端 未结 7 1169
说谎
说谎 2020-11-29 05:23

I have a scenario in Zuul where the service that the URL is routed too might be down . So the reponse body gets thrown with 500 HTTP Status and ZuulException in the JSON bod

7条回答
  •  青春惊慌失措
    2020-11-29 05:46

    We finally got this working [Coded by one of my colleague]:-

    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; // Needs to run before SendErrorFilter which has filterOrder == 0
        }
    
        @Override
        public boolean shouldFilter() {
            // only forward to errorPath if it hasn't been forwarded to already
            return RequestContext.getCurrentContext().containsKey("error.status_code");
        }
    
        @Override
        public Object run() {
            try {
                RequestContext ctx = RequestContext.getCurrentContext();
                Object e = ctx.get("error.exception");
    
                if (e != null && e instanceof ZuulException) {
                    ZuulException zuulException = (ZuulException)e;
                    LOG.error("Zuul failure detected: " + zuulException.getMessage(), zuulException);
    
                    // Remove error code to prevent further error handling in follow up filters
                    ctx.remove("error.status_code");
    
                    // Populate context with new response values
                    ctx.setResponseBody(“Overriding Zuul Exception Body”);
                    ctx.getResponse().setContentType("application/json");
                    ctx.setResponseStatusCode(500); //Can set any error code as excepted
                }
            }
            catch (Exception ex) {
                LOG.error("Exception filtering in custom error filter", ex);
                ReflectionUtils.rethrowRuntimeException(ex);
            }
            return null;
        }
    }
    

提交回复
热议问题