自定义Zuul异常 [英] Customizing Zuul Exception
问题描述
我在Zuul有一个场景,URL路由的服务也可能已关闭。因此,响应主体在JSON正文响应中被抛出500 HTTP状态和ZuulException。
{
timestamp :1459973637928,
status:500,
错误:内部服务器错误,
异常:com.netflix.zuul.exception.ZuulException,
消息:转发错误
}
我想做的就是自定义或删除JSON响应,并可能更改HTTP状态代码。
我尝试使用@ControllerAdvice创建异常处理程序,但处理程序不会抓取异常。 / p>
更新:
所以我扩展了Zuul过滤器我可以看到它进入错误执行后的run方法如何更改响应然后。以下是我到目前为止所得到的。我在某处读到了有关SendErrorFilter的内容,但是我该如何实现它以及它做了什么?
public class CustomFilter extends ZuulFilter {
@Override
public String filterType(){
returnpost;
}
@Override
public int filterOrder(){
返回1;
}
@Override
public boolean shouldFilter(){
return true;
}
@Override
public Object run(){
final RequestContext ctx = RequestContext.getCurrentContext();
final HttpServletResponse response = ctx.getResponse();
if(HttpStatus.INTERNAL_SERVER_ERROR.value()== ctx.getResponse()。getStatus()){
try {
response.sendError(404,Error Error); //尝试更改响应将需要抛出一个JSON正文。
} catch(最终IOException e){
e.printStackTrace();
};
}
返回null;
}
将此添加到具有@EnableZuulProxy
$ b $的班级b @Bean
public CustomFilter customFilter(){
return new CustomFilter();
}
我们终于有了这个工作[由我的一位同事编写]: -
公共类CustomErrorFilter扩展ZuulFilter {
私有静态最终版Logger LOG = LoggerFactory.getLogger(CustomErrorFilter.class);
@Override
public String filterType(){
returnpost;
}
@Override
public int filterOrder(){
return -1; //需要在SendErrorFilter之前运行,它具有filterOrder == 0
}
@Override
public boolean shouldFilter(){
//仅转发到errorPath,如果它尚未转发到
返回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失败:+ zuulException.getMessage(),zuulException);
//删除错误代码以防止后续过滤器中的进一步错误处理
ctx.remove(error.status_code);
//用新响应值填充上下文
ctx.setResponseBody(覆盖Zuul异常体);
ctx.getResponse()。setContentType(application / json);
ctx.setResponseStatusCode(500); //可以将任何错误代码设置为例外
}
}
catch(Exception ex){
LOG.error(自定义错误过滤器中的异常过滤,ex);
ReflectionUtils.rethrowRuntimeException(ex);
}
返回null;
}
}
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 body response.
{
"timestamp": 1459973637928,
"status": 500,
"error": "Internal Server Error",
"exception": "com.netflix.zuul.exception.ZuulException",
"message": "Forwarding error"
}
All I want to do is to customise or remove the JSON response and maybe change the HTTP status Code.
I tried to create a exception Handler with @ControllerAdvice but the exception is not grabbed by the handler.
UPDATES:
So I extended the Zuul Filter I can see it getting into the run method after the error has been executed how do i change the response then. Below is what i got so far. I read somewhere about SendErrorFilter but how do i implement that and what does it do?
public class CustomFilter extends ZuulFilter {
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
final RequestContext ctx = RequestContext.getCurrentContext();
final HttpServletResponse response = ctx.getResponse();
if (HttpStatus.INTERNAL_SERVER_ERROR.value() == ctx.getResponse().getStatus()) {
try {
response.sendError(404, "Error Error"); //trying to change the response will need to throw a JSON body.
} catch (final IOException e) {
e.printStackTrace();
} ;
}
return null;
}
Added this to the class that has @EnableZuulProxy
@Bean
public CustomFilter customFilter() {
return new CustomFilter();
}
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;
}
}
这篇关于自定义Zuul异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!