Ver código fonte

feat(exception): 添加自定义异常处理逻辑

- 新增 CommonException 类作为基础异常类
- 添加 BadRequestException、DbException 等具体异常类
- 实现 CommonExceptionAdvice 类用于全局异常处理
- 优化异常处理逻辑,提高系统稳定性和可维护性
nahida 11 meses atrás
pai
commit
599ae8fb93

+ 67 - 0
src/main/java/com/zksy/advice/CommonExceptionAdvice.java

@@ -0,0 +1,67 @@
+package com.zksy.advice;
+
+
+import com.zksy.exception.BadRequestException;
+import com.zksy.exception.CommonException;
+import com.zksy.exception.DbException;
+import com.zksy.utils.AjaxResult;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.ObjectError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.util.NestedServletException;
+
+import java.net.BindException;
+import java.util.stream.Collectors;
+
+@RestControllerAdvice
+@Slf4j
+public class CommonExceptionAdvice {
+
+    @ExceptionHandler(DbException.class)
+    public Object handleDbException(DbException e) {
+        log.error("mysql数据库操作异常 -> ", e);
+        return processResponse(e);
+    }
+
+    @ExceptionHandler(CommonException.class)
+    public Object handleBadRequestException(CommonException e) {
+        log.error("自定义异常 -> {} , 异常原因:{}  ",e.getClass().getName(), e.getMessage());
+        log.debug("", e);
+        return processResponse(e);
+    }
+
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
+        String msg = e.getBindingResult().getAllErrors()
+                .stream().map(ObjectError::getDefaultMessage)
+                .collect(Collectors.joining("|"));
+        log.error("请求参数校验异常 -> {}", msg);
+        log.debug("", e);
+        return processResponse(new BadRequestException(msg));
+    }
+    @ExceptionHandler(BindException.class)
+    public Object handleBindException(BindException e) {
+        log.error("请求参数绑定异常 ->BindException, {}", e.getMessage());
+        log.debug("", e);
+        return processResponse(new BadRequestException("请求参数格式错误"));
+    }
+
+    @ExceptionHandler(NestedServletException.class)
+    public Object handleNestedServletException(NestedServletException e) {
+        log.error("参数异常 -> NestedServletException,{}", e.getMessage());
+        log.debug("", e);
+        return processResponse(new BadRequestException("请求参数处理异常"));
+    }
+
+    @ExceptionHandler(Exception.class)
+    public Object handleRuntimeException(Exception e) {
+        return processResponse(new CommonException("服务器内部异常", 500));
+    }
+
+    private ResponseEntity<AjaxResult> processResponse(CommonException e){
+        return ResponseEntity.status(e.getCode()).body(AjaxResult.error(String.valueOf(e)));
+    }
+}

+ 16 - 0
src/main/java/com/zksy/exception/BadRequestException.java

@@ -0,0 +1,16 @@
+package com.zksy.exception;
+
+public class BadRequestException extends CommonException{
+
+    public BadRequestException(String message) {
+        super(message, 400);
+    }
+
+    public BadRequestException(String message, Throwable cause) {
+        super(message, cause, 400);
+    }
+
+    public BadRequestException(Throwable cause) {
+        super(cause, 400);
+    }
+}

+ 16 - 0
src/main/java/com/zksy/exception/BizIllegalException.java

@@ -0,0 +1,16 @@
+package com.zksy.exception;
+
+public class BizIllegalException extends CommonException{
+
+    public BizIllegalException(String message) {
+        super(message, 500);
+    }
+
+    public BizIllegalException(String message, Throwable cause) {
+        super(message, cause, 500);
+    }
+
+    public BizIllegalException(Throwable cause) {
+        super(cause, 500);
+    }
+}

+ 23 - 0
src/main/java/com/zksy/exception/CommonException.java

@@ -0,0 +1,23 @@
+package com.zksy.exception;
+
+import lombok.Getter;
+
+@Getter
+public class CommonException extends RuntimeException{
+    private int code;
+
+    public CommonException(String message, int code) {
+        super(message);
+        this.code = code;
+    }
+
+    public CommonException(String message, Throwable cause, int code) {
+        super(message, cause);
+        this.code = code;
+    }
+
+    public CommonException(Throwable cause, int code) {
+        super(cause);
+        this.code = code;
+    }
+}

+ 16 - 0
src/main/java/com/zksy/exception/DbException.java

@@ -0,0 +1,16 @@
+package com.zksy.exception;
+
+public class DbException extends CommonException{
+
+    public DbException(String message) {
+        super(message, 500);
+    }
+
+    public DbException(String message, Throwable cause) {
+        super(message, cause, 500);
+    }
+
+    public DbException(Throwable cause) {
+        super(cause, 500);
+    }
+}

+ 16 - 0
src/main/java/com/zksy/exception/ForbiddenException.java

@@ -0,0 +1,16 @@
+package com.zksy.exception;
+
+public class ForbiddenException extends CommonException{
+
+    public ForbiddenException(String message) {
+        super(message, 403);
+    }
+
+    public ForbiddenException(String message, Throwable cause) {
+        super(message, cause, 403);
+    }
+
+    public ForbiddenException(Throwable cause) {
+        super(cause, 403);
+    }
+}

+ 77 - 0
src/main/java/com/zksy/exception/ServiceException.java

@@ -0,0 +1,77 @@
+package com.zksy.exception;
+
+/**
+ * 业务异常
+ *
+ * @author ruoyi
+ */
+public final class ServiceException extends RuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 错误码
+     */
+    private Integer code;
+
+    /**
+     * 错误提示
+     */
+    private String message;
+
+    private Object[] parameter;
+
+    /**
+     * 错误明细,内部调试错误
+     * <p>
+     * 和 {@link CommonResult#getDetailMessage()} 一致的设计
+     */
+    private String detailMessage;
+
+    /**
+     * 空构造方法,避免反序列化问题
+     */
+    public ServiceException() {
+    }
+
+    public ServiceException(String message) {
+        this.message = message;
+    }
+
+    public ServiceException(String message, Integer code) {
+        this.message = message;
+        this.code = code;
+    }
+
+    public String getDetailMessage() {
+        return detailMessage;
+    }
+
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public ServiceException setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+
+    public ServiceException setDetailMessage(String detailMessage) {
+        this.detailMessage = detailMessage;
+        return this;
+    }
+
+    public ServiceException(String message, Object... parameter) {
+        super(message);
+        this.message = message;
+        this.parameter = parameter;
+    }
+
+    @Override
+    public String getMessage() {
+        if (parameter == null || message == null) {
+            return message;
+        }
+        return String.format(message, parameter);
+    }
+}

+ 16 - 0
src/main/java/com/zksy/exception/UnauthorizedException.java

@@ -0,0 +1,16 @@
+package com.zksy.exception;
+
+public class UnauthorizedException extends CommonException{
+
+    public UnauthorizedException(String message) {
+        super(message, 401);
+    }
+
+    public UnauthorizedException(String message, Throwable cause) {
+        super(message, cause, 401);
+    }
+
+    public UnauthorizedException(Throwable cause) {
+        super(cause, 401);
+    }
+}