1
0
Переглянути джерело

添加统一异常处理,断言类,引入HuTool工具

frankeleyn 2 роки тому
батько
коміт
646d8e5415

+ 1 - 1
build.gradle

@@ -91,7 +91,7 @@ repositories {
 }
 
 dependencies {
-    //implementation 'org.projectlombok:lombok:1.18.10'
+    implementation 'cn.hutool:hutool-all:5.8.12'
     compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
     annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
     implementation 'com.tencentcloudapi:tencentcloud-sdk-java:3.1.695'

+ 4 - 1
src/main/java/com/ematou/wxbase/aop/LogAspect.java

@@ -1,6 +1,6 @@
 package com.ematou.wxbase.aop;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
+import com.ematou.wxbase.exception.ServiceException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.Signature;
@@ -39,6 +39,9 @@ public class LogAspect {
             logger.info("----------------方法: " + targetMethodName + "的参数为: " + objectMapper.writeValueAsString(joinPoint.getArgs()) + "----------------");
             proceed = joinPoint.proceed();
             logger.info("----------------方法: " + targetMethodName + "执行的返回值为: " + proceed + "----------------");
+        }catch (ServiceException se){
+            // 如果时业务异常记录结果,抛出交给统一异常处理
+            throw se;
         } catch (Throwable throwable) {
             logger.error("----------------方法: " + targetMethodName + "执行出现异常,错误信息为:" + throwable.getMessage() + "----------------", throwable);
         }

+ 5 - 1
src/main/java/com/ematou/wxbase/common/web/R.java

@@ -32,7 +32,11 @@ public class R<T> implements Serializable {
     }
 
     public static <T> R<T> error(String message) {
-        return restResult(null, ResponseCodeConstant.code_100, message);
+        return restResult(null, ResponseCodeConstant.code_500, message);
+    }
+
+    public static <T> R<T> error(int code, String message) {
+        return restResult(null, code, message);
     }
 
     private static <T> R<T> restResult(T data, int code, String msg)

+ 2 - 0
src/main/java/com/ematou/wxbase/common/web/ResponseCodeConstant.java

@@ -7,6 +7,8 @@ package com.ematou.wxbase.common.web;
  */
 public class ResponseCodeConstant {
 
+    public static final int code_500 = 500;
+
     public static final int code_200 = 200;
 
     public static final int code_100 = 100;

+ 102 - 0
src/main/java/com/ematou/wxbase/exception/Assert.java

@@ -0,0 +1,102 @@
+package com.ematou.wxbase.exception;
+
+
+import cn.hutool.core.util.StrUtil;
+
+import java.util.Objects;
+
+/**
+ * 断言类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2022/12/21 10:39
+ */
+public abstract class Assert {
+
+    /**
+     * 断言对象不为空
+     * 如果对象obj为空,则抛出异常
+     * @param obj 待判断对象
+     */
+    public static void notNull(Object obj, String msg) {
+        if (Objects.isNull(obj)) {
+            throw new ServiceException(msg);
+        }
+    }
+
+
+    /**
+     * 断言对象为空
+     * 如果对象obj不为空,则抛出异常
+     * @param object
+     * @param msg
+     */
+    public static void isNull(Object object, String msg) {
+        if (Objects.nonNull(object)) {
+            throw new ServiceException(msg);
+        }
+    }
+
+    /**
+     * 断言表达式为真
+     * 如果为假,则抛出异常
+     *
+     * @param expression 是否成功
+     */
+    public static void isTrue(boolean expression, String msg) {
+        if (!expression) {
+            throw new ServiceException(msg);
+        }
+    }
+
+    /**
+     * 断言表达式为假
+     * 如果为真,则抛出异常
+     *
+     * @param expression 是否成功
+     */
+    public static void notTrue(boolean expression, String msg) {
+        if (expression) {
+            throw new ServiceException(msg);
+        }
+    }
+
+    /**
+     * 断言两个对象不相等
+     * 如果相等,则抛出异常
+     * @param m1
+     * @param m2
+     * @param msg
+     */
+    public static void notEquals(Object m1, Object m2,  String msg) {
+        if (m1.equals(m2)) {
+            throw new ServiceException(msg);
+        }
+    }
+
+    /**
+     * 断言两个对象相等
+     * 如果不相等,则抛出异常
+     * @param m1
+     * @param m2
+     * @param msg
+     */
+    public static void equals(Object m1, Object m2,  String msg) {
+        if (!m1.equals(m2)) {
+            throw new ServiceException(msg);
+        }
+    }
+
+    /**
+     * 断言参数不为空
+     * 如果为空,则抛出异常
+     * @param s
+     * @param msg
+     */
+    public static void notEmpty(String s, String msg) {
+        if (StrUtil.isBlank(s)) {
+            throw new ServiceException(msg);
+        }
+    }
+}

+ 34 - 0
src/main/java/com/ematou/wxbase/exception/ServiceException.java

@@ -0,0 +1,34 @@
+package com.ematou.wxbase.exception;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * 业务异常类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/2/18 15:36
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public final class ServiceException extends RuntimeException {
+
+    /**
+     * 错误码,默认 500
+     */
+    private Integer code = 500;
+
+    /**
+     * 错误信息
+     */
+    private String message;
+
+    public ServiceException(String message)
+    {
+        this.message = message;
+    }
+
+}

+ 46 - 0
src/main/java/com/ematou/wxbase/handler/GlobalExceptionHandler.java

@@ -0,0 +1,46 @@
+package com.ematou.wxbase.handler;
+
+import com.ematou.wxbase.common.web.R;
+import com.ematou.wxbase.exception.ServiceException;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 全局异常处理器
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/2/18 15:35
+ */
+@RestControllerAdvice
+@Slf4j
+public class GlobalExceptionHandler {
+
+    /**
+     * 业务异常
+     */
+    @ExceptionHandler(ServiceException.class)
+    public R handleServiceException(ServiceException e, HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生业务异常{}", requestURI, e.getMessage());
+        return R.error(e.getMessage());
+    }
+
+    /**
+     * 系统异常
+     */
+    @ExceptionHandler(Exception.class)
+    public R handleException(Exception e, HttpServletRequest request)
+    {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生系统异常.", requestURI, e);
+        return R.error("系统异常!");
+    }
+
+
+
+}

+ 11 - 0
src/test/java/com/ematou/wxbase/MerchInfoServiceTest.java

@@ -1,6 +1,7 @@
 package com.ematou.wxbase;
 
 import com.ematou.wxbase.entity.MerchInfo;
+import com.ematou.wxbase.mapper.MerchInfoMapper;
 import com.ematou.wxbase.service.MerchInfoService;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -19,6 +20,16 @@ public class MerchInfoServiceTest {
     @Autowired
     private MerchInfoService service;
 
+    @Autowired
+    private MerchInfoMapper mapper;
+
+    @Test
+    public void testMapperSelectOne() {
+        MerchInfo merchInfo = new MerchInfo();
+        merchInfo.setMerchSn("mhbs990053989883052032");
+        System.out.println(mapper.selectOne(merchInfo));
+    }
+
     @Test
     public void testInsert() {
         MerchInfo merchInfo = new MerchInfo();