Fork me on GitHub

Feign调用全局异常处理解决

异常信息形如:TestService#addRecord(ParamVO) failed and no fallback available.

对于failed and no fallback available.这种异常信息,是因为项目开启了熔断:

1
feign.hystrix.enabled: true

当调用服务时抛出了异常,却没有定义fallback方法,就会抛出上述异常。由此引出了第一个解决方式。

解决方案:

自定义Feign解析器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.crecgec.baseboot.jsoncore.exception.BaseException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class FeignErrorDecoder implements ErrorDecoder {

@Override
public Exception decode(String methodKey, Response response) {
try {
// 这里直接拿到我们抛出的异常信息
String message = Util.toString(response.body().asReader());
try {
JSONObject jsonObject = JSONObject.parseObject(message);
return new BaseException(jsonObject.getString("resultMsg"), jsonObject.getInteger("resultCode"));
} catch (JSONException e) {
e.printStackTrace();
}

} catch (IOException ignored) {
}
return decode(methodKey, response);
}
}

定义系统的异常类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class BaseException extends RuntimeException {
private int status ;

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public BaseException() {
}

public BaseException(String message, int status) {
super(message);
this.status = status;
}

public BaseException(String message) {
super(message);
}

public BaseException(String message, Throwable cause) {
super(message, cause);
}

public BaseException(Throwable cause) {
super(cause);
}

public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

统一异常拦截转换对应的异常信息返回前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class ResultSet {

/**
* 返回的状态码
*/
private Integer resultCode;

/**
* 返回的消息
*/
private String resultMsg;

/**
* 返回的数据
*/
private Object data;

public ResultSet() {
}

public ResultSet(Integer resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}

public ResultSet(Integer resultCode, String resultMsg, Object data) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
this.data = data;
}

public Integer getResultCode() {
return resultCode;
}

public void setResultCode(Integer resultCode) {
this.resultCode = resultCode;
}

public String getResultMsg() {
return resultMsg;
}

public void setResultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}
}

全局异常类处理配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@ExceptionHandler(value = BaseException.class)
public ResultSet defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, BaseException e) {
ResultSet resultSet = new ResultSet();

if (e.getStatus() == 400) {
resultSet.setResultCode(-1);
resultSet.setResultMsg(e.getMessage());
resultSet.setData(null);
resp.setStatus(400);
} else {
resp.setStatus(500);
if(logger.isErrorEnabled()){
logger.error("系统异常,请联系系统开发人员进行处理", e);
}
resultSet.setResultCode(-1);
resultSet.setResultMsg(e.getMessage());
resultSet.setData(null);
}

return resultSet;
}

这样就能完成了feign接收异常处理的自定义异常信息!

相关文章

微信打赏

赞赏是不耍流氓的鼓励