From 7b0a2a98cdeb7b83d0cf77fa55b6bfdff639a38a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4=E4=BA=BA?= <3076767823@qq.com>
Date: Wed, 15 Jan 2025 17:17:27 +0800
Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0controller?=
=?UTF-8?q?=E5=B1=82=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../common/exception/ControllerAdvice.java | 141 ++++++++++++++++++
.../com/dpkj/common/exception/ErrorEnum.java | 70 +++++++++
.../dpkj/common/exception/ErrorInterface.java | 23 +++
.../dpkj/common/exception/RRException.java | 59 ++++++++
src/main/java/com/dpkj/common/vo/Result.java | 9 ++
5 files changed, 302 insertions(+)
create mode 100644 src/main/java/com/dpkj/common/exception/ControllerAdvice.java
create mode 100644 src/main/java/com/dpkj/common/exception/ErrorEnum.java
create mode 100644 src/main/java/com/dpkj/common/exception/ErrorInterface.java
create mode 100644 src/main/java/com/dpkj/common/exception/RRException.java
diff --git a/src/main/java/com/dpkj/common/exception/ControllerAdvice.java b/src/main/java/com/dpkj/common/exception/ControllerAdvice.java
new file mode 100644
index 0000000..eb769f7
--- /dev/null
+++ b/src/main/java/com/dpkj/common/exception/ControllerAdvice.java
@@ -0,0 +1,141 @@
+package com.dpkj.common.exception;
+
+import com.dpkj.common.vo.Result;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Controller;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.FieldError;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.MissingServletRequestParameterException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * controller控制器异常处理接口实现类
+ *
其它module可以集成此类进行controller层的异常处理
+ * @author 石头人
+ */
+@Slf4j
+@Component
+@RestControllerAdvice(annotations = {RestController.class, Controller.class})
+public class ControllerAdvice {
+
+ /**
+ * controller方法中的参数校验失败,但是前提是要使用
+ * #@Validated注解开启参数校验
+ *
+ * @param e 异常
+ * @return Result
+ */
+ @ExceptionHandler(value = MethodArgumentNotValidException.class)
+ public Result bindingException(MethodArgumentNotValidException e) {
+ // 获得所有校验出错的返回集
+ BindingResult bindingResult = e.getBindingResult();
+ List fieldErrors = bindingResult.getFieldErrors();
+ // 循环获得所有校验异常的字段
+ Map fieldMap = new HashMap<>();
+ for (FieldError fieldError : fieldErrors) {
+ fieldMap.put(fieldError.getField(), fieldError.getDefaultMessage());
+ }
+
+ String errMsg = fieldMap.values().toString().replaceAll("]", "").replaceAll("\\[", "");
+ log.warn(errMsg);
+ // 返回给前端
+ return Result.error(errMsg);
+ }
+
+ /**
+ * 处理空指针异常
+ * @param nullPointerException 空指针异常
+ * @return Result
+ */
+ @ExceptionHandler(value = java.lang.NullPointerException.class)
+ public Result nullPointException(java.lang.NullPointerException nullPointerException) {
+ log.error("空指针异常类型: {},信息: {}", nullPointerException.getClass(),nullPointerException.getMessage());
+ return Result.error(ErrorEnum.NULL_POINTER_EXCEPTION);
+ }
+
+ /**
+ * 所有的运行时异常,抛出异常
+ * @param throwable 异常
+ * @return Result
+ */
+ @ExceptionHandler(value = Throwable.class)
+ public Result handleException(Throwable throwable) {
+ log.error("异常类型: {}, {}, 信息为: {}", throwable.getCause(), throwable.getClass(), throwable.getMessage());
+ if (throwable instanceof RRException){
+ RRException rrException = (RRException) throwable;
+ return Result.error(rrException.getCode(), rrException.getMsg());
+ }
+ return Result.error(ErrorEnum.RUNTIME_EXCEPTION);
+ }
+
+ /**
+ * http信息无可读
+ * @param e 异常
+ * @return Result
+ */
+ @ExceptionHandler(value = HttpMessageNotReadableException.class)
+ public Result httpMessageNotReadAbleException(HttpMessageNotReadableException e){
+ log.warn("异常类型: {} 无可读信息: {}", e.getClass(), e.getMessage());
+ return Result.error(ErrorEnum.HTTP_MESSAGE_NOT_READABLE_EXCEPTION);
+ }
+
+ /**
+ * 运行时异常
+ * @param e 运行异常对象
+ * @return Result
+ */
+ @ExceptionHandler(value = RuntimeException.class)
+ public Result runtimeException(RuntimeException e){
+ if (!e.getMessage().isEmpty()){
+ log.error("运行时异常 消息为: {}", e.getMessage());
+ }
+ if (e instanceof RRException){
+ RRException rrException = (RRException) e;
+ return Result.error(rrException.getCode(), rrException.getMsg());
+ }
+ return Result.error(ErrorEnum.RUNTIME_EXCEPTION);
+ }
+
+ /**
+ * 请求不支持
+ * @return Result
+ */
+ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
+ public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
+ log.warn("暂不支持该请求: {}", e.getMessage());
+ return Result.error("暂不支持此请求方式");
+ }
+
+ /**
+ * 参数类型错误
+ * @return Result
+ */
+ @ExceptionHandler(MethodArgumentTypeMismatchException.class)
+ public Result methodArgument(MethodArgumentTypeMismatchException e){
+ log.warn("参数发生错误: {}", e.getMessage());
+ return Result.error("参数发生错误");
+ }
+
+ /**
+ * 缺少请求参数
+ * @param e 缺少请求参数异常
+ * @return Result
+ */
+ @ExceptionHandler(MissingServletRequestParameterException.class)
+ public Result exception(MissingServletRequestParameterException e){
+ log.warn("缺少请求参数: {}", e.getMessage());
+ return Result.error("缺少请求参数");
+ }
+
+}
diff --git a/src/main/java/com/dpkj/common/exception/ErrorEnum.java b/src/main/java/com/dpkj/common/exception/ErrorEnum.java
new file mode 100644
index 0000000..0cd4742
--- /dev/null
+++ b/src/main/java/com/dpkj/common/exception/ErrorEnum.java
@@ -0,0 +1,70 @@
+package com.dpkj.common.exception;
+
+import lombok.Getter;
+
+
+/**
+ * 错误返回枚举类
+ */
+@Getter
+public enum ErrorEnum implements ErrorInterface{
+
+// ==========================================================================
+
+ /**
+ * 成功范围
+ * @code 200
+ * @apiNote 访问成功
+ */
+ SUCCESS(200, "访问成功"),
+
+ /**
+ * 系统异常
+ * @code 500
+ * @apiNote 系统异常
+ */
+ FAIL(500, "系统异常"),
+
+ /**
+ * 所有的空指针异常
+ * @code 10002
+ * @apiNote 填写信息为空
+ */
+ NULL_POINTER_EXCEPTION(501, "填写信息为空"),
+
+ /**
+ * 运行时异常,
+ * @code 10003
+ * @apiNote 系统发生错误,请联系管理员
+ */
+ RUNTIME_EXCEPTION(502, "系统发生错误,请联系管理员"),
+
+ /**
+ * Http传入的参数没有可以读的数据
+ * @code 10004
+ * @apiNote 传入的数据不可读
+ */
+ HTTP_MESSAGE_NOT_READABLE_EXCEPTION(503, "传入的数据不可读"),
+
+
+
+// ==========================================================================
+
+ ;
+ private final Integer code;
+ private final String message;
+ ErrorEnum(Integer code, String message){
+ this.message = message;
+ this.code = code;
+ }
+
+ @Override
+ public int getCode(){
+ return this.code;
+ }
+
+ @Override
+ public String getMessage(){
+ return this.message;
+ }
+}
diff --git a/src/main/java/com/dpkj/common/exception/ErrorInterface.java b/src/main/java/com/dpkj/common/exception/ErrorInterface.java
new file mode 100644
index 0000000..c610dfe
--- /dev/null
+++ b/src/main/java/com/dpkj/common/exception/ErrorInterface.java
@@ -0,0 +1,23 @@
+package com.dpkj.common.exception;
+
+/**
+ * 错误枚举接口,所有的错误枚举都需要实现该接口,如果需要自定义
+ * 错误信息,可通过实现该接口与RespBean进行使用
+ *
+ * @author 石头人
+ * @see com.dpkj.common.vo.Result
+ * @since 2023-07-27 13:05:00
+ */
+public interface ErrorInterface {
+
+ /**
+ * 获取响应码
+ */
+ int getCode();
+
+ /**
+ * 获取响应信息
+ */
+ String getMessage();
+
+}
diff --git a/src/main/java/com/dpkj/common/exception/RRException.java b/src/main/java/com/dpkj/common/exception/RRException.java
new file mode 100644
index 0000000..ebfcafc
--- /dev/null
+++ b/src/main/java/com/dpkj/common/exception/RRException.java
@@ -0,0 +1,59 @@
+package com.dpkj.common.exception;
+
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * 总的异常,所有的自定义异常都需要继承类
+ *
+ * @author 石头人
+ * @since 2023-07-24 09:42:00
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+public class RRException extends RuntimeException{
+
+ private static final long serialVersionUID = 1L;
+
+ private String msg;
+ private int code = 500;
+
+ public RRException(){
+ super("系统错误");
+ this.msg = "系统错误";
+ }
+
+ public RRException(Throwable throwable){
+ super(throwable);
+ }
+
+ public RRException(String msg) {
+ super(msg);
+ this.msg = msg;
+ }
+
+ public RRException(String msg, Throwable e) {
+ super(msg, e);
+ this.msg = msg;
+ }
+
+ public RRException(int code, String msg) {
+ super(msg);
+ this.msg = msg;
+ this.code = code;
+ }
+ public RRException(ErrorInterface error){
+ super(error.getMessage());
+ this.code = error.getCode();
+ this.msg = error.getMessage();
+ }
+
+ public RRException(int code,String msg, Throwable e) {
+ super(msg, e);
+ this.msg = msg;
+ this.code = code;
+ }
+
+}
+
diff --git a/src/main/java/com/dpkj/common/vo/Result.java b/src/main/java/com/dpkj/common/vo/Result.java
index 47c5e90..ed17053 100644
--- a/src/main/java/com/dpkj/common/vo/Result.java
+++ b/src/main/java/com/dpkj/common/vo/Result.java
@@ -1,6 +1,7 @@
package com.dpkj.common.vo;
import com.dpkj.common.constant.CommonConst;
+import com.dpkj.common.exception.ErrorEnum;
import lombok.Data;
import java.io.Serializable;
@@ -69,6 +70,14 @@ public class Result implements Serializable {
return error(CommonConst.SC_500, msg, null);
}
+ public static Result error(ErrorEnum errorEnum) {
+ return error(errorEnum.getCode(), errorEnum.getMessage(), null);
+ }
+
+ public static Result error(int code, String message) {
+ return error(code, message, null);
+ }
+
public static Result error(int code, String msg, T data) {
Result r = new Result();
r.setCode(code);