如何捕获 FeignClient 异常
How to catch FeignClient exception
我尝试捕获从 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);
}
结果,当我得到 <--- HTTP/1.1 400 Bad Request (5380ms)
我有默认错误消息
HttpStatus.INTERNAL_SERVER_ERROR, 100, 100,
throwable.getMessage());
但没有扩展自定义异常,我尝试在 CustomErrorDecoder 中设置。
我做错了什么,为什么我不能调用 RetAppiException 和 return 休息客户端的错误答案。
谢谢。
你无法用 @ControllerAdvice
捕获 FeignClient 的异常。异常处理程序将不会捕获假客户端生成的异常,错误解码器..
一个简单的解决方案是捕获你的假调用,然后抛出你想要的异常。
try{
feignClient.method();
} catch(Exception ex){
//throw exceptions you want
throw new YourException();
}
那你就可以搞定了:
@ControllerAdvice
public class GlobalControllerAdvice {
private final Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(YourException.class)
public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
//impl
}
}
您可以通过捕获 HystrixRuntimeException
并将 getCause()
转换为 FeignClientException
来捕获假客户端异常。
示例:
@ExceptionHandler
public ResponseEntity<Problem> handleFeignException(HystrixRuntimeException ex, NativeWebRequest request) {
final FeignException.FeignClientException feignClientException = (FeignException.FeignClientException) ex.getCause();
return handleException(feignClientException, request);
}
我尝试捕获从 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);
}
结果,当我得到 <--- HTTP/1.1 400 Bad Request (5380ms) 我有默认错误消息
HttpStatus.INTERNAL_SERVER_ERROR, 100, 100, throwable.getMessage());
但没有扩展自定义异常,我尝试在 CustomErrorDecoder 中设置。
我做错了什么,为什么我不能调用 RetAppiException 和 return 休息客户端的错误答案。
谢谢。
你无法用 @ControllerAdvice
捕获 FeignClient 的异常。异常处理程序将不会捕获假客户端生成的异常,错误解码器..
一个简单的解决方案是捕获你的假调用,然后抛出你想要的异常。
try{
feignClient.method();
} catch(Exception ex){
//throw exceptions you want
throw new YourException();
}
那你就可以搞定了:
@ControllerAdvice
public class GlobalControllerAdvice {
private final Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(YourException.class)
public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
//impl
}
}
您可以通过捕获 HystrixRuntimeException
并将 getCause()
转换为 FeignClientException
来捕获假客户端异常。
示例:
@ExceptionHandler
public ResponseEntity<Problem> handleFeignException(HystrixRuntimeException ex, NativeWebRequest request) {
final FeignException.FeignClientException feignClientException = (FeignException.FeignClientException) ex.getCause();
return handleException(feignClientException, request);
}