Browse Source

代码优化

wangliang 2 years ago
parent
commit
4697f96c16

+ 0 - 39
teachcloud-common/src/main/java/com/qmth/teachcloud/common/enums/ExceptionResultEnum.java

@@ -1,15 +1,8 @@
 package com.qmth.teachcloud.common.enums;
 
 import com.qmth.boot.api.exception.ApiException;
-import com.qmth.teachcloud.common.contant.SystemConstant;
-import com.qmth.teachcloud.common.util.ServletUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 
-import javax.servlet.http.HttpServletRequest;
-import java.util.StringJoiner;
-
 /**
  * @Description: 自定义异常消息枚举
  * @Param:
@@ -145,46 +138,14 @@ public enum ExceptionResultEnum {
     }
 
     public ApiException exception() {
-        try {
-            StringJoiner stringJoiner = new StringJoiner("");
-            HttpServletRequest request = ServletUtil.getRequest();
-            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
-            stringJoiner.add("[status]:").add(String.valueOf(status.value())).add("\r\n");
-            stringJoiner.add("[code]:").add(String.valueOf(code)).add("\r\n");
-            stringJoiner.add("[message]:").add(message);
-            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
-        } catch (Exception e) {
-        }
         return new ApiException(status, code, message, message);
     }
 
-    private final static Logger log = LoggerFactory.getLogger(ExceptionResultEnum.class);
-
     public ApiException exception(int code, String message) {
-        try {
-            StringJoiner stringJoiner = new StringJoiner("");
-            HttpServletRequest request = ServletUtil.getRequest();
-            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
-            stringJoiner.add("[status]:").add(String.valueOf(status.value())).add("\r\n");
-            stringJoiner.add("[code]:").add(String.valueOf(code)).add("\r\n");
-            stringJoiner.add("[message]:").add(message);
-            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
-        } catch (Exception e) {
-        }
         return new ApiException(status, code, message, message);
     }
 
     public ApiException exception(String message) {
-        try {
-            StringJoiner stringJoiner = new StringJoiner("");
-            HttpServletRequest request = ServletUtil.getRequest();
-            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
-            stringJoiner.add("[status]:").add(String.valueOf(status.value())).add("\r\n");
-            stringJoiner.add("[code]:").add(String.valueOf(code)).add("\r\n");
-            stringJoiner.add("[message]:").add(message);
-            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
-        } catch (Exception e) {
-        }
         return new ApiException(status, code, message, message);
     }
 }

+ 30 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ResultUtil.java

@@ -34,6 +34,16 @@ public class ResultUtil {
     }
 
     public static Result error() {
+        try {
+            StringJoiner stringJoiner = new StringJoiner("");
+            HttpServletRequest request = ServletUtil.getRequest();
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getStatus().value())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getCode())).add("\r\n");
+            stringJoiner.add("[message]:").add(ExceptionResultEnum.EXCEPTION_ERROR.getMessage());
+            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
+        } catch (Exception e) {
+        }
         throw ExceptionResultEnum.EXCEPTION_ERROR.exception();
     }
 
@@ -52,10 +62,30 @@ public class ResultUtil {
     }
 
     public static Result error(ExceptionResultEnum e, int code, String message) {
+        try {
+            StringJoiner stringJoiner = new StringJoiner("");
+            HttpServletRequest request = ServletUtil.getRequest();
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(e.getStatus().value())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(code)).add("\r\n");
+            stringJoiner.add("[message]:").add(message);
+            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
+        } catch (Exception e1) {
+        }
         throw e.exception(code, message);
     }
 
     public static Result error(String message) {
+        try {
+            StringJoiner stringJoiner = new StringJoiner("");
+            HttpServletRequest request = ServletUtil.getRequest();
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.ERROR.getStatus().value())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.ERROR.getCode())).add("\r\n");
+            stringJoiner.add("[message]:").add(message);
+            log.error(SystemConstant.LOG_ERROR, stringJoiner.toString());
+        } catch (Exception e) {
+        }
         throw ExceptionResultEnum.ERROR.exception(message);
     }