因項(xiàng)目重構(gòu)采用spring cloud,feign不可避免施逾。目前spring cloud在國內(nèi)還不是很成熟,所以踩坑是免不了的例获。最近處理全局異常的問題汉额,搜了個遍也沒找到合適的解決方案。最后自己采取了一個不是perfect的方案處理了榨汤。終究問題還是解決了蠕搜。
首先系統(tǒng)定義了兩個異常類:
@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 + "\"" +"}";
}
}
內(nèi)部異常類如下:
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內(nèi)部拋出異常不進(jìn)行處理,F(xiàn)eign獲取spring默認(rèn)包裝異常結(jié)果如下:
{
"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("系統(tǒng)異常,請聯(lián)系管理員");
}
}
總結(jié):
@controllerAdvice或者HandlerExceptionResolver是不能直接捕獲到FeignException,所以需要在Feign層面拿到具體異常重新封裝收壕。最后總算把cloud service內(nèi)部的異常安全(一樣的錯誤碼妓灌、一樣的錯誤信息)送給了client!!