增加 CommonResult 的 check 逻辑

This commit is contained in:
YunaiV
2022-06-17 23:54:42 +08:00
parent ff18d2f30a
commit ca28d791aa
38 changed files with 221 additions and 699 deletions

View File

@@ -19,6 +19,7 @@
<module>yudao-spring-boot-starter-file</module>
<module>yudao-spring-boot-starter-monitor</module>
<module>yudao-spring-boot-starter-protection</module>
<!-- <module>yudao-spring-boot-starter-config</module>-->
<module>yudao-spring-boot-starter-job</module>
<module>yudao-spring-boot-starter-mq</module>
<module>yudao-spring-boot-starter-rpc</module>
@@ -29,12 +30,15 @@
<module>yudao-spring-boot-starter-biz-operatelog</module>
<module>yudao-spring-boot-starter-biz-dict</module>
<module>yudao-spring-boot-starter-biz-sms</module>
<module>yudao-spring-boot-starter-activiti</module>
<module>yudao-spring-boot-starter-biz-pay</module>
<module>yudao-spring-boot-starter-biz-weixin</module>
<module>yudao-spring-boot-starter-biz-social</module>
<module>yudao-spring-boot-starter-biz-tenant</module>
<module>yudao-spring-boot-starter-biz-data-permission</module>
<module>yudao-spring-boot-starter-biz-error-code</module>
<module>yudao-spring-boot-starter-activiti</module>
<module>yudao-spring-boot-starter-flowable</module>
</modules>

View File

@@ -0,0 +1,60 @@
package cn.iocoder.yudao.framework.common.exception;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 服务器异常 Exception
*/
@Data
@EqualsAndHashCode(callSuper = true)
public final class ServerException extends RuntimeException {
/**
* 全局错误码
*
* @see GlobalErrorCodeConstants
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 空构造方法,避免反序列化问题
*/
public ServerException() {
}
public ServerException(ErrorCode errorCode) {
this.code = errorCode.getCode();
this.message = errorCode.getMsg();
}
public ServerException(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public ServerException setCode(Integer code) {
this.code = code;
return this;
}
@Override
public String getMessage() {
return message;
}
public ServerException setMessage(String message) {
this.message = message;
return this;
}
}

View File

@@ -36,9 +36,15 @@ public interface GlobalErrorCodeConstants {
ErrorCode UNKNOWN = new ErrorCode(999, "未知错误");
static boolean isMatch(Integer code) {
/**
* 是否为服务端错误,参考 HTTP 5XX 错误码段
*
* @param code 错误码
* @return 是否
*/
static boolean isServerErrorCode(Integer code) {
return code != null
&& code >= SUCCESS.getCode() && code <= UNKNOWN.getCode();
&& code >= INTERNAL_SERVER_ERROR.getCode() && code <= INTERNAL_SERVER_ERROR.getCode() + 99;
}
}

View File

@@ -95,6 +95,16 @@ public class CommonResult<T> implements Serializable {
throw new ServiceException(code, msg);
}
/**
* 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
* 如果没有,则返回 {@link #data} 数据
*/
@JsonIgnore // 避免 jackson 序列化
public T getCheckedData() {
checkError();
return data;
}
public static <T> CommonResult<T> error(ServiceException serviceException) {
return error(serviceException.getCode(), serviceException.getMessage());
}

View File

@@ -106,9 +106,7 @@ public class DeptDataPermissionRule implements DataPermissionRule {
DeptDataPermissionRespDTO deptDataPermission = loginUser.getContext(CONTEXT_KEY, DeptDataPermissionRespDTO.class);
// 从上下文中拿不到,则调用逻辑进行获取
if (deptDataPermission == null) {
CommonResult<DeptDataPermissionRespDTO> getDeptDataPermissionResult = permissionApi.getDeptDataPermission(loginUser.getId());
getDeptDataPermissionResult.checkError();
deptDataPermission = getDeptDataPermissionResult.getData();
deptDataPermission = permissionApi.getDeptDataPermission(loginUser.getId()).getData();
if (deptDataPermission == null) {
log.error("[getExpression][LoginUser({}) 获取数据权限为 null]", JsonUtils.toJsonString(loginUser));
throw new NullPointerException(String.format("LoginUser(%d) Table(%s/%s) 未返回数据权限",

View File

@@ -34,9 +34,8 @@ public class DictFrameworkUtils {
@Override
public DictDataRespDTO load(KeyValue<String, String> key) {
CommonResult<DictDataRespDTO> getDictDataResult = dictDataApi.getDictData(key.getKey(), key.getValue());
getDictDataResult.checkError();
return ObjectUtil.defaultIfNull(getDictDataResult.getData(), DICT_DATA_NULL);
return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()).getCheckedData(),
DICT_DATA_NULL);
}
});
@@ -50,9 +49,8 @@ public class DictFrameworkUtils {
@Override
public DictDataRespDTO load(KeyValue<String, String> key) {
CommonResult<DictDataRespDTO> parseDictDataResult = dictDataApi.parseDictData(key.getKey(), key.getValue());
parseDictDataResult.checkError();
return ObjectUtil.defaultIfNull(parseDictDataResult.getData(), DICT_DATA_NULL);
return ObjectUtil.defaultIfNull(dictDataApi.parseDictData(key.getKey(), key.getValue()).getCheckedData(),
DICT_DATA_NULL);
}
});

View File

@@ -23,8 +23,7 @@ public class OperateLogFrameworkServiceImpl implements OperateLogFrameworkServic
@Async
public void createOperateLog(OperateLog operateLog) {
OperateLogCreateReqDTO reqDTO = BeanUtil.copyProperties(operateLog, OperateLogCreateReqDTO.class);
CommonResult<Boolean> result = operateLogApi.createOperateLog(reqDTO);
result.checkError();
operateLogApi.createOperateLog(reqDTO).checkError();
}
}

View File

@@ -30,9 +30,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService {
@Override
public List<Long> load(Object key) {
CommonResult<List<Long>> tenantIdsResult = tenantApi.getTenantIds();
tenantIdsResult.checkError();
return tenantIdsResult.getData();
return tenantApi.getTenantIds().getCheckedData();
}
});

View File

@@ -77,9 +77,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
private LoginUser buildLoginUserByToken(String token, Integer userType) {
try {
// 校验访问令牌
CommonResult<OAuth2AccessTokenCheckRespDTO> accessTokenResult = oauth2TokenApi.checkAccessToken(token);
accessTokenResult.checkError();
OAuth2AccessTokenCheckRespDTO accessToken = accessTokenResult.getData();
OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token).getCheckedData();
if (accessToken == null) {
return null;
}

View File

@@ -38,13 +38,12 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyRolesCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofMinutes(1L), // 过期时间 1 分钟
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() {
@Override
public Boolean load(KeyValue<Long, List<String>> key) {
CommonResult<Boolean> hasAnyRolesResult = permissionApi.hasAnyRoles(key.getKey(),
key.getValue().toArray(new String[0]));
hasAnyRolesResult.checkError();
return hasAnyRolesResult.getData();
return permissionApi.hasAnyRoles(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData();
}
});
/**
@@ -53,13 +52,12 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyPermissionsCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofMinutes(1L), // 过期时间 1 分钟
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() {
@Override
public Boolean load(KeyValue<Long, List<String>> key) {
CommonResult<Boolean> hasAnyPermissionsResult = permissionApi.hasAnyPermissions(key.getKey(),
key.getValue().toArray(new String[0]));
hasAnyPermissionsResult.checkError();
return hasAnyPermissionsResult.getData();
return permissionApi.hasAnyPermissions(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData();
}
});
@Override

View File

@@ -23,8 +23,7 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe
@Async
public void createApiAccessLog(ApiAccessLog apiAccessLog) {
ApiAccessLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiAccessLog, ApiAccessLogCreateReqDTO.class);
CommonResult<Boolean> result = apiAccessLogApi.createApiAccessLog(reqDTO);
result.checkError();
apiAccessLogApi.createApiAccessLog(reqDTO).checkError();
}
}

View File

@@ -23,8 +23,7 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ
@Async
public void createApiErrorLog(ApiErrorLog apiErrorLog) {
ApiErrorLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiErrorLog, ApiErrorLogCreateReqDTO.class);
CommonResult<Boolean> result = apiErrorLogApi.createApiErrorLog(reqDTO);
result.checkError();
apiErrorLogApi.createApiErrorLog(reqDTO).checkError();
}
}