【更新】新增MyBatisSystemException异常拦截处理

This commit is contained in:
徐玉祥 2022-10-04 19:14:51 +08:00 committed by 小诺
parent 99e8ff82cc
commit 62d6c38113
4 changed files with 27 additions and 9 deletions

View File

@ -68,6 +68,7 @@ public class ClientUser extends CommonEntity {
/** 性别 */
@ApiModelProperty(value = "性别", position = 8)
@TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
@Trans(type = TransType.DICTIONARY, key = "GENDER")
private String gender;
/** 年龄 */

View File

@ -17,6 +17,8 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
@ -113,6 +115,21 @@ public class GlobalExceptionUtil {
// 如果是SaToken相关异常则由AuthExceptionUtil处理
return AuthExceptionUtil.getCommonResult(e);
} else if(e instanceof MyBatisSystemException) {
// 如果是MyBatisSystemException
Throwable cause = e.getCause();
if (cause instanceof PersistenceException) {
Throwable secondCause = cause.getCause();
if (secondCause instanceof CommonException) {
CommonException commonException = (CommonException) secondCause;
commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null);
} else {
commonResult = CommonResult.error("数据操作异常");
}
}else {
commonResult = CommonResult.error("数据操作异常");
}
} else if (e instanceof CommonException) {
// 通用业务异常直接返回给前端