|
@@ -4,11 +4,15 @@ import cn.dev33.satoken.exception.NotLoginException;
|
|
|
import cn.dev33.satoken.exception.NotPermissionException;
|
|
|
import cn.dev33.satoken.jwt.exception.SaJwtException;
|
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
+import com.xs.core.common.annotation.OperationLog;
|
|
|
import com.xs.core.common.exception.BusinessException;
|
|
|
import com.xs.core.model.ResponseResult;
|
|
|
+import com.xs.core.service.log.ISysAppLogService;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import jakarta.validation.ConstraintViolationException;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
@@ -20,19 +24,25 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
|
|
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
|
|
+import org.springframework.web.servlet.HandlerMapping;
|
|
|
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@RestControllerAdvice
|
|
|
@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
public class GlobalExceptionHandler {
|
|
|
+ private final ISysAppLogService sysLogService;
|
|
|
+
|
|
|
@ExceptionHandler({Exception.class, NotLoginException.class, HttpMessageNotReadableException.class,
|
|
|
HttpMediaTypeNotSupportedException.class, MaxUploadSizeExceededException.class, NotPermissionException.class,
|
|
|
- HttpRequestMethodNotSupportedException.class, MethodArgumentNotValidException.class, BindException.class,
|
|
|
- ConstraintViolationException.class, MissingServletRequestPartException.class, SaJwtException.class, NoResourceFoundException.class, BusinessException.class})
|
|
|
+ HttpRequestMethodNotSupportedException.class, ConstraintViolationException.class, MissingServletRequestPartException.class, SaJwtException.class, NoResourceFoundException.class, BusinessException.class})
|
|
|
@ResponseBody
|
|
|
ResponseResult<?> serverExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
|
|
|
if (e instanceof NotLoginException || e instanceof SaJwtException) {
|
|
@@ -53,7 +63,7 @@ public class GlobalExceptionHandler {
|
|
|
String field = fieldError.getField();
|
|
|
String defaultMessage = fieldError.getDefaultMessage();
|
|
|
// 尝试从消息源获取国际化消息,如果没有找到,则使用默认消息
|
|
|
- return ResponseResult.failed(defaultMessage,null);
|
|
|
+ return ResponseResult.failed(defaultMessage, null);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -72,4 +82,44 @@ public class GlobalExceptionHandler {
|
|
|
return ResponseResult.serverError(null);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
|
|
+ public ResponseResult<?> handleValidationException(Exception ex, HttpServletRequest request) {
|
|
|
+ // 获取方法上的日志注解
|
|
|
+ HandlerMethod handlerMethod = (HandlerMethod) request.getAttribute(
|
|
|
+ HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
|
|
|
+ OperationLog operationLog = handlerMethod.getMethodAnnotation(OperationLog.class);
|
|
|
+
|
|
|
+ Method method = handlerMethod.getMethod();
|
|
|
+ String methodName = method.getDeclaringClass().getName() + "." + method.getName();
|
|
|
+
|
|
|
+ ResponseResult<Object> failed = ResponseResult.failed("response.validation.failed", null);
|
|
|
+ List<FieldError> fieldErrorList = ((BindException) ex).getFieldErrors();
|
|
|
+ if (!CollectionUtils.isEmpty(fieldErrorList)) {
|
|
|
+ for (FieldError fieldError : fieldErrorList) {
|
|
|
+ if (fieldError != null && fieldError.getDefaultMessage() != null) {
|
|
|
+ String defaultMessage = fieldError.getDefaultMessage();
|
|
|
+ // 尝试从消息源获取国际化消息,如果没有找到,则使用默认消息
|
|
|
+ failed = ResponseResult.failed(defaultMessage, null);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ if (operationLog != null) {
|
|
|
+ sysLogService.saveAppLog(
|
|
|
+ operationLog.description(),
|
|
|
+ operationLog.operationType(),
|
|
|
+ operationLog.module(),
|
|
|
+ methodName,
|
|
|
+ request.getParameterMap(),
|
|
|
+ failed,
|
|
|
+ 0,
|
|
|
+ ex
|
|
|
+ );
|
|
|
+ }
|
|
|
+ // 返回错误信息
|
|
|
+ return failed;
|
|
|
+ }
|
|
|
}
|