因项目重构采用spring cloud,feign不可避免。目前spring cloud在国内还不是很成熟,所以踩坑是免不了的。最近处理全局异常的问题,搜了个遍也没找到合适的解决方案。最后自己采取了一个不是perfect的方案处理了。终究问题还是解决了。
首先系统定义了两个异常类:
@Data
public class BusinessException extends RuntimeException{
private Integer code;
private String message;
public BusinessException() {
}
public BusinessException(String errmsg) {
super(errmsg);
this.code=2;
this.message=errmsg;
}
public BusinessException(ExceptionEnum exceptionEnum) {
super(exceptionEnum.getMsg());
this.code=exceptionEnum.getCode();
this.message=exceptionEnum.getMsg();
}
public BusinessException(int code ,String errmsg) {
super(errmsg);
this.code=code;
this.message=errmsg;
}
@Override
public String toString() {
return "{" +
"\"code\":" + code +
", \"message\":\"" + message + "\"" +"}";
}
}
内部异常类如下:
public class InternalException extends RuntimeException{
public InternalException(String msg) {
super(msg);
}
}
采用@controllerAdvice全局捕获异常
/**
* 全局异常处理类
*/
@Slf4j
@ControllerAdvice(basePackages = {"捕获哪些包"})
public class ExceptionHandle {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResultResponse handle(Exception e) {
if (e instanceof BusinessException) {
BusinessException businessException = (BusinessException) e;
if(businessException.getCode()==null){
return ResultResponse.error(businessException.getMessage());
}
return ResultResponse.error(businessException.getCode(),businessException.getMessage());
}else{
return ResultResponse.error(e.getMessage());
}
}
}
cloud内部抛出异常不进行处理,Feign获取spring默认包装异常结果如下:
{
"timestamp": "2017-12-27 15:01:53",
"status": 500,
"error": "Internal Server Error",
"exception": "com.keruyun.loyalty.commons.master.exception.BusinessException",
"message": "Request processing failed; nested exception is {\"code\":1000, \"message\":\"test Exception\"}",
"path": "/coupon/cloud/commercial/8469"
}
重写Feign的ErrorDecoder
@Configuration
@Slf4j
public class ExceptionErrorDecoder implements ErrorDecoder {
ObjectMapper objectMapper = new ObjectMapper();
@Override
public Exception decode(String s, Response response) {
try {
if (response.body() != null) {
String targetMsg = null;
String body = Util.toString(response.body().asReader());
log.error(body);
ExceptionInfo ei = this.objectMapper.readValue(body.getBytes("UTF-8"), ExceptionInfo.class);
Class clazz = Class.forName(ei.getException());
Object obj = clazz.newInstance();
String message = ei.getMessage();
if (obj instanceof BusinessException) {
targetMsg = message.substring(message.indexOf("{"), message.indexOf("}") + 1);
BusinessException businessException = JsonUtil.toObj(targetMsg, BusinessException.class);
return businessException;
}else{
targetMsg=message.substring(message.indexOf(":"),message.length());
return new InternalException(targetMsg);
}
}
} catch (Exception var4) {
log.error(var4.getMessage());
return new InternalException(var4.getMessage());
}
return new InternalException("系统异常,请联系管理员");
}
}
总结:
@controllerAdvice或者HandlerExceptionResolver是不能直接捕获到FeignException,所以需要在Feign层面拿到具体异常重新封装。最后总算把cloud service内部的异常安全(一样的错误码、一样的错误信息)送给了client!!