|
@@ -0,0 +1,73 @@
|
|
|
|
|
+package com.tzld.supply.handle;
|
|
|
|
|
+
|
|
|
|
|
+import com.tzld.supply.common.base.CommonResponse;
|
|
|
|
|
+import com.tzld.supply.common.enums.ExceptionEnum;
|
|
|
|
|
+import com.tzld.supply.common.exception.CommonException;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.validation.BindException;
|
|
|
|
|
+import org.springframework.validation.ObjectError;
|
|
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 全局异常处理器
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+@RestControllerAdvice
|
|
|
|
|
+public class GlobalExceptionHandle {
|
|
|
|
|
+
|
|
|
|
|
+ private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandle.class);
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler
|
|
|
|
|
+ public Object handleException(HttpServletRequest req, Exception exception) throws Exception {
|
|
|
|
|
+ String uri = req.getRequestURI();
|
|
|
|
|
+ CommonResponse<Object> response = new CommonResponse<Object>();
|
|
|
|
|
+ // 业务异常
|
|
|
|
|
+ if (exception instanceof CommonException) {
|
|
|
|
|
+ CommonException e = (CommonException) exception;
|
|
|
|
|
+ response.setCode(e.getCode());
|
|
|
|
|
+ response.setMsg(e.getMsg());
|
|
|
|
|
+ LOGGER.info("uri:" + uri + "\n" + "CustomException log.", exception);
|
|
|
|
|
+ } else if (exception instanceof MethodArgumentNotValidException) {
|
|
|
|
|
+ // 参数校验异常
|
|
|
|
|
+ MethodArgumentNotValidException e = (MethodArgumentNotValidException) exception;
|
|
|
|
|
+ List<ObjectError> errorList = e.getBindingResult().getAllErrors();
|
|
|
|
|
+ StringBuilder errorMsg = new StringBuilder();
|
|
|
|
|
+ errorMsg.append("|");
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(errorList)) {
|
|
|
|
|
+ for (ObjectError objectError : errorList) {
|
|
|
|
|
+ errorMsg.append(objectError.getDefaultMessage()).append("|");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
|
|
|
|
|
+ response.setMsg(errorMsg.toString());
|
|
|
|
|
+ LOGGER.error("uri:" + uri + "\n" + "MethodArgumentNotValidException log.", exception);
|
|
|
|
|
+ } else if (exception instanceof BindException) {
|
|
|
|
|
+ // 参数绑定异常
|
|
|
|
|
+ BindException e = (BindException) exception;
|
|
|
|
|
+ List<ObjectError> errorList = e.getBindingResult().getAllErrors();
|
|
|
|
|
+ StringBuilder errorMsg = new StringBuilder();
|
|
|
|
|
+ errorMsg.append("|");
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(errorList)) {
|
|
|
|
|
+ for (ObjectError objectError : errorList) {
|
|
|
|
|
+ errorMsg.append(objectError.getDefaultMessage()).append("|");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ response.setCode(ExceptionEnum.PARAM_ERROR.getCode());
|
|
|
|
|
+ response.setMsg(errorMsg.toString());
|
|
|
|
|
+ LOGGER.error("uri:" + uri + "\n" + "BindException log.", exception);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ response.setCode(ExceptionEnum.SYSTEM_ERROR.getCode());
|
|
|
|
|
+ response.setMsg(ExceptionEnum.SYSTEM_ERROR.getMsg());
|
|
|
|
|
+ LOGGER.error("uri:" + uri + "\n" + "unknownException log.", exception);
|
|
|
|
|
+ }
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|