Browse Source

代码优化

wangliang 2 years ago
parent
commit
42d3b695e8

+ 1 - 2
themis-admin/src/main/java/com/qmth/themis/admin/aspect/ApiControllerAspect.java

@@ -1,6 +1,5 @@
 package com.qmth.themis.admin.aspect;
 
-import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.util.JacksonUtil;
 import com.qmth.themis.business.util.ServletUtil;
 import com.qmth.themis.common.exception.BusinessException;
@@ -73,7 +72,7 @@ public class ApiControllerAspect {
             }
             return joinPoint.proceed();
         } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
+//            log.error(SystemConstant.LOG_ERROR, e);
             if (e instanceof BusinessException) {
                 return ResultUtil.error((BusinessException) e, e.getMessage());
             } else {

+ 73 - 2
themis-common/src/main/java/com/qmth/themis/common/exception/GlobalDefultExceptionHandler.java

@@ -11,11 +11,16 @@ 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 org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.SQLException;
 import java.util.List;
+import java.util.Objects;
+import java.util.StringJoiner;
 import java.util.stream.Collectors;
 
 /**
@@ -42,22 +47,64 @@ public class GlobalDefultExceptionHandler {
     @ResponseBody
     public <T> Result defultExcepitonHandler(Exception e, HttpServletResponse response) {
         log.error("Global exception", e);
+        StringJoiner stringJoiner = new StringJoiner("");
+        HttpServletRequest request = null;
+        try {
+            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
+                    .getRequestAttributes();
+            request = Objects.nonNull(servletRequestAttributes) ? servletRequestAttributes.getRequest() : null;
+        } catch (Exception e1) {
+        }
         if (e instanceof BusinessException) {
             BusinessException businessException = (BusinessException) e;
             response.setStatus(businessException.getCode());
+            try {
+                stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+                stringJoiner.add("[status]:").add(String.valueOf(businessException.getStatusCode())).add("\r\n");
+                stringJoiner.add("[code]:").add(String.valueOf(businessException.getCode())).add("\r\n");
+                stringJoiner.add("[message]:").add(businessException.getMessage());
+                log.error("请求出错:{}", stringJoiner.toString());
+            } catch (Exception e1) {
+            }
             return ResultUtil.error(businessException.getStatusCode(), businessException.getMessage());
         } else if (e instanceof IllegalArgumentException) {
             response.setStatus(ExceptionResultEnum.EXCEPTION_ERROR.getCode());
+            String errorMessage = "暂不支持此平台";
             if (e.getMessage().contains("No enum constant com.qmth.themis.common.enums.Platform")) {
-                return ResultUtil.error(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode(), "暂不支持此平台");
+                try {
+                    stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+                    stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode())).add("\r\n");
+                    stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getCode())).add("\r\n");
+                    stringJoiner.add("[message]:").add(errorMessage);
+                    log.error("请求出错:{}", stringJoiner.toString());
+                } catch (Exception e1) {
+                }
+                return ResultUtil.error(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode(), errorMessage);
             }
         } else if (e instanceof MethodArgumentNotValidException) {
             response.setStatus(ExceptionResultEnum.EXCEPTION_ERROR.getCode());
             BindingResult bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
             List<String> errorList = bindingResult.getFieldErrors().stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
-            return ResultUtil.error(ExceptionResultEnum.ERROR.getCode(), JSONObject.toJSONString(errorList));
+            String errorMessage = JSONObject.toJSONString(errorList);
+            try {
+                stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+                stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.ERROR.getStatusCode())).add("\r\n");
+                stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.ERROR.getCode())).add("\r\n");
+                stringJoiner.add("[message]:").add(errorMessage);
+                log.error("请求出错:{}", stringJoiner.toString());
+            } catch (Exception e1) {
+            }
+            return ResultUtil.error(ExceptionResultEnum.ERROR.getCode(), errorMessage);
         }
         response.setStatus(ExceptionResultEnum.EXCEPTION_ERROR.getCode());
+        try {
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.EXCEPTION_ERROR.getCode())).add("\r\n");
+            stringJoiner.add("[message]:").add(e.getMessage());
+            log.error("请求出错:{}", stringJoiner.toString());
+        } catch (Exception e1) {
+        }
         return ResultUtil.error(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode(), e.getMessage());
     }
 
@@ -74,6 +121,18 @@ public class GlobalDefultExceptionHandler {
     public <T> Result sqlExceptionHandler(SQLException e, HttpServletResponse response) {
         response.setStatus(ExceptionResultEnum.SQL_ERROR.getCode());
         log.error("SQLException 请求出错", e);
+        try {
+            StringJoiner stringJoiner = new StringJoiner("");
+            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
+                    .getRequestAttributes();
+            HttpServletRequest request = Objects.nonNull(servletRequestAttributes) ? servletRequestAttributes.getRequest() : null;
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.SQL_ERROR.getStatusCode())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.SQL_ERROR.getCode())).add("\r\n");
+            stringJoiner.add("[message]:").add(e.getMessage());
+            log.error("请求出错:{}", stringJoiner.toString());
+        } catch (Exception e1) {
+        }
         return ResultUtil.error(ExceptionResultEnum.SQL_ERROR.getStatusCode(), e.getMessage());
     }
 
@@ -90,6 +149,18 @@ public class GlobalDefultExceptionHandler {
     public <T> Result invocationTargetExceptionHandler(InvocationTargetException e, HttpServletResponse response) {
         response.setStatus(ExceptionResultEnum.INVOCATIONTARGET_ERROR.getCode());
         log.error("invocationTargetExceptionHandler 请求出错", e);
+        try {
+            StringJoiner stringJoiner = new StringJoiner("");
+            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
+                    .getRequestAttributes();
+            HttpServletRequest request = Objects.nonNull(servletRequestAttributes) ? servletRequestAttributes.getRequest() : null;
+            stringJoiner.add("[error path]:").add(request.getServletPath()).add("\r\n");
+            stringJoiner.add("[status]:").add(String.valueOf(ExceptionResultEnum.INVOCATIONTARGET_ERROR.getStatusCode())).add("\r\n");
+            stringJoiner.add("[code]:").add(String.valueOf(ExceptionResultEnum.INVOCATIONTARGET_ERROR.getCode())).add("\r\n");
+            stringJoiner.add("[message]:").add(ExceptionResultEnum.INVOCATIONTARGET_ERROR.name() + e.getTargetException());
+            log.error("请求出错:{}", stringJoiner.toString());
+        } catch (Exception e1) {
+        }
         return ResultUtil.error(ExceptionResultEnum.INVOCATIONTARGET_ERROR.getStatusCode(),
                 ExceptionResultEnum.INVOCATIONTARGET_ERROR.name() + e.getTargetException());
     }

+ 1 - 1
themis-exam/src/main/java/com/qmth/themis/exam/aspect/ApiControllerAspect.java

@@ -74,7 +74,7 @@ public class ApiControllerAspect {
             }
             return joinPoint.proceed();
         } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
+//            log.error(SystemConstant.LOG_ERROR, e);
             if (e instanceof BusinessException) {
                 return ResultUtil.error((BusinessException) e, e.getMessage());
             } else {