WANG hace 6 años
padre
commit
324910b606

+ 54 - 17
src/main/java/cn/com/qmth/examcloud/web/support/CustomExceptionHandler.java → src/main/java/cn/com/qmth/ocean/web/support/CustomExceptionHandler.java

@@ -1,4 +1,4 @@
-package cn.com.qmth.examcloud.web.support;
+package cn.com.qmth.ocean.web.support;
 
 import java.util.List;
 import java.util.Set;
@@ -7,6 +7,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.converter.HttpMessageNotReadableException;
@@ -22,14 +23,19 @@ import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
+import cn.com.qmth.examcloud.web.cloud.AppSelfHolder;
 import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
+import cn.com.qmth.examcloud.web.exception.SequenceLockException;
+import cn.com.qmth.examcloud.web.jpa.DataIntegrityViolationTransverter;
+import cn.com.qmth.examcloud.web.support.ApiInfo;
+import cn.com.qmth.examcloud.web.support.StatusResponse;
 
 /**
- * 类注释
+ * 异常转换器
  *
  * @author WANGWEI
- * @date 2018年8月14
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ * @date 2019年1月30
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  */
 @ControllerAdvice
 @ResponseBody
@@ -57,13 +63,23 @@ public class CustomExceptionHandler {
 		StatusResponse body = null;
 
 		if (null == cause) {
-			body = new StatusResponse("500", "系统异常");
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "系统异常");
 			cause = e;
 		} else if (cause instanceof StatusException) {
 			StatusException se = (StatusException) cause;
-			body = new StatusResponse(se.getCode(), se.getDesc());
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-" + se.getCode(),
+					se.getDesc());
+		} else if (cause instanceof DataIntegrityViolationException) {
+			try {
+				DataIntegrityViolationTransverter
+						.throwIfDuplicateEntry((DataIntegrityViolationException) cause);
+				body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "数据完整性错误");
+			} catch (StatusException se) {
+				body = new StatusResponse(se.getCode(), se.getDesc());
+			}
 		} else if (cause instanceof IllegalStateException) {
-			body = new StatusResponse("500", cause.getMessage());
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+					cause.getMessage());
 		} else if (cause instanceof javax.validation.ConstraintViolationException) {
 			javax.validation.ConstraintViolationException cvExcp = (ConstraintViolationException) cause;
 			Set<ConstraintViolation<?>> constraintViolations = cvExcp.getConstraintViolations();
@@ -78,11 +94,15 @@ public class CustomExceptionHandler {
 					errorMsg.append("; ").append(cv.getMessage());
 				}
 			}
-			body = new StatusResponse("500", errorMsg.toString());
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+					errorMsg.toString());
+		} else if (cause instanceof SequenceLockException) {
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+					cause.getMessage());
 		} else if (cause instanceof org.springframework.jdbc.CannotGetJdbcConnectionException) {
-			body = new StatusResponse("500", "网络拥堵,请稍后重试");
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "网络拥堵,请稍后重试");
 		} else {
-			body = new StatusResponse("500", "系统异常");
+			body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "系统异常");
 			cause = e;
 		}
 
@@ -112,7 +132,8 @@ public class CustomExceptionHandler {
 				errorMsg.append("; ").append(err.getDefaultMessage());
 			}
 		}
-		StatusResponse body = new StatusResponse("500", errorMsg.toString());
+		StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+				errorMsg.toString());
 		return asResult(e, body, request);
 	}
 
@@ -128,7 +149,8 @@ public class CustomExceptionHandler {
 	public ResponseEntity<StatusResponse> handleException(MissingServletRequestParameterException e,
 			HttpServletRequest request) {
 
-		StatusResponse body = new StatusResponse("500", e.getMessage());
+		StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+				e.getMessage());
 		return asResult(e, body, request);
 	}
 
@@ -144,7 +166,8 @@ public class CustomExceptionHandler {
 	public ResponseEntity<StatusResponse> handleException(HttpMessageNotReadableException e,
 			HttpServletRequest request) {
 
-		StatusResponse body = new StatusResponse("500", "Required request body is missing");
+		StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+				"Required request body is missing");
 		return asResult(e, body, request);
 	}
 
@@ -159,7 +182,7 @@ public class CustomExceptionHandler {
 	@ExceptionHandler(Exception.class)
 	public ResponseEntity<StatusResponse> handleException(Exception e, HttpServletRequest request) {
 
-		StatusResponse body = new StatusResponse("500", "致命错误");
+		StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "致命错误");
 
 		return asResult(e, body, request);
 	}
@@ -176,13 +199,27 @@ public class CustomExceptionHandler {
 	private ResponseEntity<StatusResponse> asResult(Throwable t, StatusResponse body,
 			HttpServletRequest request) {
 		boolean alwaysOK = alwaysOK(request);
+		ApiInfo apiInfo = (ApiInfo) request
+				.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
+		boolean withoutStackTrace = false;
+		if (null != apiInfo) {
+			withoutStackTrace = apiInfo.isWithoutStackTrace();
+		}
 		if (alwaysOK) {
 			INTERFACE_LOG.error("[HTTP-RESP]. status=" + HttpStatus.OK.value());
-			INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body), t);
+			if (withoutStackTrace) {
+				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body));
+			} else {
+				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body), t);
+			}
 			return new ResponseEntity<StatusResponse>(body, HttpStatus.OK);
 		} else {
 			INTERFACE_LOG.error("[HTTP-RESP]. status=" + HttpStatus.INTERNAL_SERVER_ERROR.value());
-			INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body), t);
+			if (withoutStackTrace) {
+				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body));
+			} else {
+				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body), t);
+			}
 			return new ResponseEntity<StatusResponse>(body, HttpStatus.INTERNAL_SERVER_ERROR);
 		}
 	}
@@ -207,4 +244,4 @@ public class CustomExceptionHandler {
 		return alwaysOK;
 	}
 
-}
+}