Quellcode durchsuchen

捕获异常代码合并

small vor 2 Jahren
Ursprung
Commit
d83cc75ab5

+ 88 - 3
sckw-common/sckw-common-core/src/main/java/com/sckw/core/exception/GlobalSystemExceptionHandler.java

@@ -2,14 +2,20 @@ package com.sckw.core.exception;
 
 import com.sckw.core.web.constant.HttpStatus;
 import com.sckw.core.web.response.HttpResult;
+import jakarta.validation.ConstraintViolation;
+import jakarta.validation.ConstraintViolationException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.util.CollectionUtils;
+import org.springframework.validation.FieldError;
 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 java.util.List;
 import java.util.Objects;
+import java.util.Set;
 
 @Slf4j
 @RestControllerAdvice
@@ -22,10 +28,89 @@ public class GlobalSystemExceptionHandler {
         return HttpResult.error(e.getCode(), e.getMessage());
     }
 
-    @ExceptionHandler(value = MethodArgumentNotValidException.class)
+    /**
+     * BusinessException处理
+     *
+     * @param ex
+     * @return
+     */
     @ResponseBody
-    public HttpResult handlerRuntimeException(MethodArgumentNotValidException e) {
-        return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE , Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
+    @ExceptionHandler(BusinessException.class)
+    public HttpResult businessExceptionHandler(BusinessException ex) {
+        log.info("业务异常,message={},param={}", ex.getMsg(), ex.getParam());
+        return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, ex.getMessage());
+    }
+
+    /**
+     * NotLoginException处理
+     *
+     * @param ex
+     * @return
+     */
+    @ResponseBody
+    @ExceptionHandler(NotLoginException.class)
+    public HttpResult notLoginException(NotLoginException ex) {
+        log.info("用户未登录, message={}, param={}", ex.getMsg(), ex.getParam());
+        return HttpResult.error(HttpStatus.UN_LOGIN_CODE, HttpStatus.UN_LOGIN_MESSAGE, ex.getMessage());
+    }
+
+
+    /**
+     * 注解校验异常处理
+     *
+     * @param ex
+     * @return
+     */
+    @ResponseBody
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public HttpResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
+        List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
+        StringBuilder sb = new StringBuilder();
+        if (!CollectionUtils.isEmpty(fieldErrors)) {
+            boolean first = true;
+            for (FieldError fieldError : fieldErrors) {
+                if (!first) {
+                    sb.append(",");
+                }
+                sb.append(fieldError.getDefaultMessage());
+                first = false;
+            }
+        }
+        String errMsg = sb.toString();
+        log.info("参数校验异常m:{}", errMsg);
+        return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, errMsg);
+    }
+
+    @ResponseBody
+    @ExceptionHandler(ConstraintViolationException.class)
+    public HttpResult constraintViolationExceptionHandler(ConstraintViolationException ex) {
+        log.info("参数校验异常c:{}", ex.getMessage());
+        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
+        StringBuilder sb = new StringBuilder();
+        if (!CollectionUtils.isEmpty(constraintViolations)) {
+            boolean first = true;
+            for (ConstraintViolation<?> constraintViolation : constraintViolations) {
+                if (!first) {
+                    sb.append(",");
+                }
+                sb.append(constraintViolation.getMessage());
+                first = false;
+            }
+        }
+        return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, sb.toString());
+    }
+
+    /**
+     * exception处理
+     *
+     * @param ex
+     * @returnh
+     */
+    @ResponseBody
+    @ExceptionHandler(Exception.class)
+    public HttpResult defaultExceptionHandler(Exception ex) {
+        log.error("系统异常", ex);
+        return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
     }
 
 }