Browse Source

修改:

1.ResultDataResolver中的逻辑
dev
wangxianzhang 4 years ago
parent
commit
36f7f9471d
  1. 55
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/ResultDataResolver.java

55
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/ResultDataResolver.java

@ -1,9 +1,12 @@
package com.epmet.commons.tools.feign; package com.epmet.commons.tools.feign;
import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.Result;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Feign请求结果解析器 * Feign请求结果解析器
@ -12,30 +15,30 @@ public interface ResultDataResolver {
/** /**
* @Description 获取Result种的data如果失败(返回result为null或者result.success为false)那么返回null * @Description 获取Result种的data如果失败(返回result为null或者result.success为false)那么返回null
* @return * @return data数据
* @author wxz * @author wxz
* @date 2021.06.07 22:45 * @date 2021.06.07 22:45
*/ */
//default <R> R tryGetResultData(Result<R> result, String targetServiceName) { default <R> R getResultDataOrReturnNull(Result<R> result, String targetServiceName) {
// Logger logger = LoggerFactory.getLogger(ResultDataResolver.class); Logger logger = LoggerFactory.getLogger(ResultDataResolver.class);
// if (result == null) { if (result == null) {
// logger.error("调用{}服务发生错误,返回Result为null", targetServiceName); logger.error("调用{}服务发生错误,返回Result为null", targetServiceName);
// return null; return null;
// } }
// if (!result.success()) { if (!result.success()) {
// logger.error("调用{}服务发生错误,错误信息:{}", targetServiceName, result.getInternalMsg()); logger.error("调用{}服务发生错误,内部信息:{},错误信息:{}", targetServiceName, result.getInternalMsg(), result.getMsg());
// return null; return null;
// } }
// return result.getData(); return result.getData();
//} }
/** /**
* @Description * @Description 解析Result中的结果如果请求上游服务返回的结果不成功则抛出异常
* @return * @return data数据
* @param targetServiceName 目标service名称 * @param targetServiceName 目标service名称
* @param errorCode 错误码可以为空为空则使用上游服务抛出的错误码 * @param errorCode 错误码可以为空为空则使用8000
* @param errorInternalMsg 内部错误信息可以为空为空则使用上游服务抛出的异常信息 * @param errorInternalMsg 内部错误信息可以为空为空则internalMsg=""
* @param showMsg 展示给前端程序的错误信息可以为空为空则根据errorCode给定错误msg信息 * @param showMsg 展示给前端程序的错误信息可以为空为空则showMsg=""
* @author wxz * @author wxz
* @date 2021.06.07 22:45 * @date 2021.06.07 22:45
*/ */
@ -43,11 +46,25 @@ public interface ResultDataResolver {
if (result == null) { if (result == null) {
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用{}服务发生错误,返回Result为null", targetServiceName); throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用{}服务发生错误,返回Result为null", targetServiceName);
} }
if (!result.success()) {
// 考虑到:上游服务抛出的异常代码和错误消息并不一定适用于当前服务,并且上有服务的错误消息弹出之后可能给用户造成困扰,
// 因此,当前服务抛出异常的时候,不再继承上游服务返回的错误码和错误消息
/*if (!result.success()) {
Integer finalErrorCode = errorCode == null ? result.getCode() : errorCode; Integer finalErrorCode = errorCode == null ? result.getCode() : errorCode;
String finalErrorInternalMsg = StringUtils.isBlank(errorInternalMsg) ? result.getInternalMsg() : errorInternalMsg; String finalErrorInternalMsg = StringUtils.isBlank(errorInternalMsg) ? result.getInternalMsg() : errorInternalMsg;
throw new RenException(finalErrorCode, finalErrorInternalMsg, showMsg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL); throw new RenException(finalErrorCode, finalErrorInternalMsg, showMsg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL);
}*/
if (!result.success()) {
// 如果不通过参数指定code,则默认使用8000服务器开小差
Integer finalErrorCode = errorCode == null ? EpmetErrorCode.SERVER_ERROR.getCode() : errorCode;
String finalErrorInternalMsg = StringUtils.isBlank(errorInternalMsg) ? "" : errorInternalMsg;
String finalShowMsg = StringUtils.isBlank(showMsg) ? "" : showMsg;
throw new EpmetException(finalErrorCode, finalErrorInternalMsg, finalShowMsg);
} }
return result.getData(); return result.getData();
} }

Loading…
Cancel
Save