【优化】将异常处理工具类的代码进行了简化,减少不必要代码

pull/221/head
laifeng 2024-06-17 16:38:18 +08:00
parent ae92f96e6c
commit a265256f4e
2 changed files with 11 additions and 22 deletions

View File

@ -29,31 +29,26 @@ public class AuthExceptionUtil {
**/
public static CommonResult<String> getCommonResult(Exception e) {
CommonResult<String> commonResult;
if (e instanceof NotLoginException) {
if (e instanceof NotLoginException notLoginException) {
// 如果是未登录异常 401
NotLoginException notLoginException = (NotLoginException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_UNAUTHORIZED, notLoginException.getMessage(), null);
} else if (e instanceof NotRoleException) {
} else if (e instanceof NotRoleException notRoleException) {
// 如果是角色异常 403
NotRoleException notRoleException = (NotRoleException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "无此角色:" + notRoleException.getRole() +
",接口地址:" + CommonServletUtil.getRequest().getServletPath(), null);
} else if (e instanceof NotPermissionException) {
} else if (e instanceof NotPermissionException notPermissionException) {
// 如果是权限异常 403
NotPermissionException notPermissionException = (NotPermissionException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "无此权限:" + notPermissionException.getPermission(), null);
} else if (e instanceof DisableServiceException) {
} else if (e instanceof DisableServiceException disableServiceException) {
// 如果是被封禁异常 403
DisableServiceException disableServiceException = (DisableServiceException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_FORBIDDEN, "账号被封禁:" + disableServiceException.getDisableTime() + "秒后解封", null);
} else if (e instanceof SaTokenException) {
} else if (e instanceof SaTokenException saTokenException) {
// 如果是SaToken异常 直接返回
SaTokenException saTokenException = (SaTokenException) e;
commonResult = CommonResult.error(saTokenException.getMessage());
} else {
// 未知异常才打印

View File

@ -81,25 +81,21 @@ public class GlobalExceptionUtil {
log.error(">>> 参数传递格式异常:", e);
// 如果是JSON参数格式错误异常 415
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null);
} else if (e instanceof MethodArgumentNotValidException) {
} else if (e instanceof MethodArgumentNotValidException methodArgumentNotValidException) {
// 如果是参数校验异常MethodArgumentNotValidException 415
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(methodArgumentNotValidException.getBindingResult()), null);
} else if (e instanceof BindException) {
} else if (e instanceof BindException bindException) {
// 如果是参数校验异常BindException 415
BindException bindException = (BindException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(bindException.getBindingResult()), null);
} else if (e instanceof ConstraintViolationException) {
} else if (e instanceof ConstraintViolationException constraintViolationException) {
// 如果是参数校验异常ConstraintViolationException 415
ConstraintViolationException constraintViolationException = (ConstraintViolationException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, getArgNotValidMessage(constraintViolationException.getConstraintViolations()), null);
} else if (e instanceof MissingServletRequestParameterException) {
} else if (e instanceof MissingServletRequestParameterException missingServletRequestParameterException) {
// 如果是参数校验异常MissingServletRequestParameterException 415
MissingServletRequestParameterException missingServletRequestParameterException = (MissingServletRequestParameterException) e;
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, missingServletRequestParameterException.getMessage(), null);
}
else if (e instanceof MultipartException) {
@ -120,8 +116,7 @@ public class GlobalExceptionUtil {
Throwable cause = e.getCause();
if (cause instanceof PersistenceException) {
Throwable secondCause = cause.getCause();
if (secondCause instanceof CommonException) {
CommonException commonException = (CommonException) secondCause;
if (secondCause instanceof CommonException commonException) {
commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null);
} else {
log.error(">>> 数据操作异常:", e);
@ -131,10 +126,9 @@ public class GlobalExceptionUtil {
log.error(">>> 数据操作异常:", e);
commonResult = CommonResult.error("数据操作异常");
}
} else if (e instanceof CommonException) {
} else if (e instanceof CommonException commonException) {
// 通用业务异常,直接返回给前端
CommonException commonException = (CommonException) e;
commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null);
} else {
// 未知异常打印详情