【发布时间】:2017-03-14 17:24:10
【问题描述】:
我尝试捕获从 FeignClient 连接的另一个微服务收到的异常。我已经制作了自定义 ErrorDecoder,并且
public class CustomErrorDecoder implements ErrorDecoder {
private final Logger log = LoggerFactory.getLogger(getClass());
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
log.info("----------------- "+methodKey+" , "+response.status()+" , "+ response.reason());
return new RestApiException(99,99,"");
}
return defaultErrorDecoder.decode(methodKey, response);
}
}
RestApiException 扩展异常的地方。
@ControllerAdvice
public class GlobalControllerAdvice {
private final Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(RestApiException.class)
public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
log.error("Sending error to client ( "+req.getUserPrincipal().getName()+" ) \"{}\"", exception.getErrMsg());
return new ResponseEntity<RestApiException>(exception, exception.getStatus());
}
@ExceptionHandler(Throwable.class)
public ResponseEntity<RestApiException> handleException(Throwable throwable, HttpServletRequest req) {
RestApiException exception=new RestApiException(HttpStatus.INTERNAL_SERVER_ERROR, 100, 100,
throwable.getMessage());
return handleException(exception, req);
}
结果,当我得到
HttpStatus.INTERNAL_SERVER_ERROR, 100, 100, throwable.getMessage());
但不是expexted自定义异常,我尝试在CustomErrorDecoder中设置。
我做错了什么,为什么我不能调用 RetAppiException 并将错误答案返回给休息客户端。
谢谢。
【问题讨论】:
-
因为您一遍又一遍地调用相同的方法...您一遍又一遍地调用
handleException(throwable)方法。另一种方法具有不同的签名handleException(exception, request),但您没有调用它。 -
我认为主要原因是 (RestApiException.java:25) ~[classes/:na].,如果我阻止这个调用代码 (@ExceptionHandler(Throwable.class)),我会得到相同的结果没有 ciclong 调用的错误。
-
不,这不是......您的问题实际上是您一遍又一遍地调用相同的方法。
-
我添加了有问题的额外信息。
-
我放弃了。显然,您一遍又一遍地调用相同的方法,无论您提供多少信息,这种情况仍在发生。