Sfoglia il codice sorgente

全局异常处理

15902849627 2 anni fa
parent
commit
0652dfe7b6

+ 4 - 0
sckw-common/sckw-common-core/pom.xml

@@ -98,6 +98,10 @@
             <artifactId>sckw-common-remote</artifactId>
             <version>1.0.0</version>
         </dependency>
+        <dependency>
+            <groupId>jakarta.validation</groupId>
+            <artifactId>jakarta.validation-api</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 30 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/exception/BusinessException.java

@@ -0,0 +1,30 @@
+package com.sckw.core.exception;
+
+import lombok.Getter;
+
+/**
+ * @Author yzc
+ * @Description 自定义业务异常
+ * @createTime 2023年06月08日 10:05:00
+ */
+@Getter
+public class BusinessException extends RuntimeException {
+
+    /**
+     * 异常信息
+     **/
+    private String msg;
+    private Object[] param;
+
+    public BusinessException(String msg) {
+        super(msg);
+        this.msg = msg;
+    }
+
+    public BusinessException(String msg, Object... param) {
+        super(msg);
+        this.msg = msg;
+        this.param = param;
+    }
+
+}

+ 29 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/exception/NotLoginException.java

@@ -0,0 +1,29 @@
+package com.sckw.core.exception;
+
+import lombok.Getter;
+
+/**
+ * @Author yzc
+ * @Description 未登陆异常
+ * @createTime 2023年06月08日 10:05:00
+ */
+@Getter
+public class NotLoginException extends  RuntimeException {
+
+    /**
+     * 异常信息
+    **/
+    private String msg;
+    private Object[] param;
+
+    public NotLoginException(String msg) {
+        super(msg);
+        this.msg = msg;
+    }
+    public NotLoginException(String msg, Object... param){
+        super(msg);
+        this.msg = msg;
+        this.param=param;
+    }
+
+}

+ 113 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/global/GlobalExceptionHandler.java

@@ -0,0 +1,113 @@
+package com.sckw.core.global;
+
+import com.sckw.core.exception.BusinessException;
+import com.sckw.core.exception.NotLoginException;
+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.util.CollectionUtils;
+import org.springframework.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @Author yzc
+ * @Description 统一异常处理
+ * @createTime 2023年06月08日 10:59:00
+ */
+@Slf4j
+@ControllerAdvice(basePackages = "com.sckw.*.controller")
+public class GlobalExceptionHandler {
+
+    /**
+     * exception处理
+     *
+     * @param ex
+     * @return
+     */
+    @ResponseBody
+    @ExceptionHandler(Exception.class)
+    public HttpResult defaultExceptionHandler(Exception ex) {
+        log.error("系统异常", ex);
+        return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
+    }
+
+    /**
+     * BusinessException处理
+     *
+     * @param ex
+     * @return
+     */
+    @ResponseBody
+    @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());
+    }
+}