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