Merge branch 'master-jdk17' of https://gitee.com/zhijiantianya/yudao-cloud
# Conflicts: # yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.javapull/137/MERGE
commit
d97e54b5cd
|
@ -4,6 +4,7 @@ import cn.hutool.core.exceptions.ExceptionUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||||
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
|
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
|
||||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||||
|
@ -14,8 +15,14 @@ import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
||||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
||||||
|
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.validation.ConstraintViolation;
|
||||||
|
import jakarta.validation.ConstraintViolationException;
|
||||||
|
import jakarta.validation.ValidationException;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
|
@ -27,16 +34,18 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.validation.ConstraintViolation;
|
|
||||||
import javax.validation.ConstraintViolationException;
|
|
||||||
import javax.validation.ValidationException;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.METHOD_NOT_ALLOWED;
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
|
* 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
|
||||||
|
@ -88,6 +97,9 @@ public class GlobalExceptionHandler {
|
||||||
if (ex instanceof NoHandlerFoundException) {
|
if (ex instanceof NoHandlerFoundException) {
|
||||||
return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex);
|
return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex);
|
||||||
}
|
}
|
||||||
|
if (ex instanceof NoResourceFoundException) {
|
||||||
|
return noResourceFoundExceptionHandler(request, (NoResourceFoundException) ex);
|
||||||
|
}
|
||||||
if (ex instanceof HttpRequestMethodNotSupportedException) {
|
if (ex instanceof HttpRequestMethodNotSupportedException) {
|
||||||
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
|
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
|
||||||
}
|
}
|
||||||
|
@ -118,7 +130,7 @@ public class GlobalExceptionHandler {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
public CommonResult<?> methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
|
public CommonResult<?> methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
|
||||||
log.warn("[missingServletRequestParameterExceptionHandler]", ex);
|
log.warn("[methodArgumentTypeMismatchExceptionHandler]", ex);
|
||||||
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", ex.getMessage()));
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", ex.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,6 +156,22 @@ public class GlobalExceptionHandler {
|
||||||
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getDefaultMessage()));
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getDefaultMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 SpringMVC 请求参数类型错误
|
||||||
|
*
|
||||||
|
* 例如说,接口上设置了 @RequestBody实体中 xx 属性类型为 Integer,结果传递 xx 参数类型为 String
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
|
public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) {
|
||||||
|
log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex);
|
||||||
|
if(ex.getCause() instanceof InvalidFormatException) {
|
||||||
|
InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
|
||||||
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
|
||||||
|
}else {
|
||||||
|
return defaultExceptionHandler(ServletUtils.getRequest(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理 Validator 校验不通过产生的异常
|
* 处理 Validator 校验不通过产生的异常
|
||||||
*/
|
*/
|
||||||
|
@ -177,6 +205,15 @@ public class GlobalExceptionHandler {
|
||||||
return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getRequestURL()));
|
return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getRequestURL()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 SpringMVC 请求地址不存在
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(NoResourceFoundException.class)
|
||||||
|
private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException ex) {
|
||||||
|
log.warn("[noResourceFoundExceptionHandler]", ex);
|
||||||
|
return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getResourcePath()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理 SpringMVC 请求方法不正确
|
* 处理 SpringMVC 请求方法不正确
|
||||||
*
|
*
|
||||||
|
@ -278,12 +315,12 @@ public class GlobalExceptionHandler {
|
||||||
errorLog.setApplicationName(applicationName);
|
errorLog.setApplicationName(applicationName);
|
||||||
errorLog.setRequestUrl(request.getRequestURI());
|
errorLog.setRequestUrl(request.getRequestURI());
|
||||||
Map<String, Object> requestParams = MapUtil.<String, Object>builder()
|
Map<String, Object> requestParams = MapUtil.<String, Object>builder()
|
||||||
.put("query", ServletUtils.getParamMap(request))
|
.put("query", JakartaServletUtil.getParamMap(request))
|
||||||
.put("body", ServletUtils.getBody(request)).build();
|
.put("body", JakartaServletUtil.getBody(request)).build();
|
||||||
errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
|
errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
|
||||||
errorLog.setRequestMethod(request.getMethod());
|
errorLog.setRequestMethod(request.getMethod());
|
||||||
errorLog.setUserAgent(ServletUtils.getUserAgent(request));
|
errorLog.setUserAgent(ServletUtils.getUserAgent(request));
|
||||||
errorLog.setUserIp(ServletUtils.getClientIP(request));
|
errorLog.setUserIp(JakartaServletUtil.getClientIP(request));
|
||||||
errorLog.setExceptionTime(LocalDateTime.now());
|
errorLog.setExceptionTime(LocalDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ public interface DictTypeConstants {
|
||||||
// ========== SYSTEM 模块 ==========
|
// ========== SYSTEM 模块 ==========
|
||||||
|
|
||||||
String USER_SEX = "system_user_sex"; // 用户性别
|
String USER_SEX = "system_user_sex"; // 用户性别
|
||||||
|
String DATA_SCOPE = "system_data_scope"; // 数据范围
|
||||||
|
|
||||||
String LOGIN_TYPE = "system_login_type"; // 登录日志的类型
|
String LOGIN_TYPE = "system_login_type"; // 登录日志的类型
|
||||||
String LOGIN_RESULT = "system_login_result"; // 登录结果
|
String LOGIN_RESULT = "system_login_result"; // 登录结果
|
||||||
|
|
|
@ -27,10 +27,10 @@ public interface ErrorCodeConstants {
|
||||||
// ========== 角色模块 1-002-002-000 ==========
|
// ========== 角色模块 1-002-002-000 ==========
|
||||||
ErrorCode ROLE_NOT_EXISTS = new ErrorCode(1_002_002_000, "角色不存在");
|
ErrorCode ROLE_NOT_EXISTS = new ErrorCode(1_002_002_000, "角色不存在");
|
||||||
ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1_002_002_001, "已经存在名为【{}】的角色");
|
ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1_002_002_001, "已经存在名为【{}】的角色");
|
||||||
ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1_002_002_002, "已经存在编码为【{}】的角色");
|
ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1_002_002_002, "已经存在标识为【{}】的角色");
|
||||||
ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1_002_002_003, "不能操作类型为系统内置的角色");
|
ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1_002_002_003, "不能操作类型为系统内置的角色");
|
||||||
ErrorCode ROLE_IS_DISABLE = new ErrorCode(1_002_002_004, "名字为【{}】的角色已被禁用");
|
ErrorCode ROLE_IS_DISABLE = new ErrorCode(1_002_002_004, "名字为【{}】的角色已被禁用");
|
||||||
ErrorCode ROLE_ADMIN_CODE_ERROR = new ErrorCode(1_002_002_005, "编码【{}】不能使用");
|
ErrorCode ROLE_ADMIN_CODE_ERROR = new ErrorCode(1_002_002_005, "标识【{}】不能使用");
|
||||||
|
|
||||||
// ========== 用户模块 1-002-003-000 ==========
|
// ========== 用户模块 1-002-003-000 ==========
|
||||||
ErrorCode USER_USERNAME_EXISTS = new ErrorCode(1_002_003_000, "用户账号已经存在");
|
ErrorCode USER_USERNAME_EXISTS = new ErrorCode(1_002_003_000, "用户账号已经存在");
|
||||||
|
|
|
@ -46,7 +46,8 @@ public class RoleRespVO {
|
||||||
private String remark;
|
private String remark;
|
||||||
|
|
||||||
@Schema(description = "数据范围,参见 DataScopeEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "数据范围,参见 DataScopeEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
@ExcelProperty("数据范围")
|
@ExcelProperty(value = "数据范围", converter = DictConvert.class)
|
||||||
|
@DictFormat(DictTypeConstants.DATA_SCOPE)
|
||||||
private Integer dataScope;
|
private Integer dataScope;
|
||||||
|
|
||||||
@Schema(description = "数据范围(指定部门数组)", example = "1")
|
@Schema(description = "数据范围(指定部门数组)", example = "1")
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package cn.iocoder.yudao.module.system.controller.admin.permission.vo.role;
|
package cn.iocoder.yudao.module.system.controller.admin.permission.vo.role;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||||
import com.mzt.logapi.starter.annotation.DiffLogField;
|
import com.mzt.logapi.starter.annotation.DiffLogField;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -23,7 +25,7 @@ public class RoleSaveReqVO {
|
||||||
|
|
||||||
@NotBlank(message = "角色标志不能为空")
|
@NotBlank(message = "角色标志不能为空")
|
||||||
@Size(max = 100, message = "角色标志长度不能超过 100 个字符")
|
@Size(max = 100, message = "角色标志长度不能超过 100 个字符")
|
||||||
@Schema(description = "角色编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "ADMIN")
|
@Schema(description = "角色标志", requiredMode = Schema.RequiredMode.REQUIRED, example = "ADMIN")
|
||||||
@DiffLogField(name = "角色标志")
|
@DiffLogField(name = "角色标志")
|
||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
|
@ -32,7 +34,14 @@ public class RoleSaveReqVO {
|
||||||
@DiffLogField(name = "显示顺序")
|
@DiffLogField(name = "显示顺序")
|
||||||
private Integer sort;
|
private Integer sort;
|
||||||
|
|
||||||
|
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
|
||||||
|
@DiffLogField(name = "状态")
|
||||||
|
@NotNull(message = "状态不能为空")
|
||||||
|
@InEnum(value = CommonStatusEnum.class, message = "状态必须是 {value}")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
@Schema(description = "备注", example = "我是一个角色")
|
@Schema(description = "备注", example = "我是一个角色")
|
||||||
|
@Size(max = 500, message = "备注长度不能超过 500 个字符")
|
||||||
@DiffLogField(name = "备注")
|
@DiffLogField(name = "备注")
|
||||||
private String remark;
|
private String remark;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue