|
@@ -1,5 +1,6 @@
|
|
|
package com.backendsys.exception;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.backendsys.utils.response.Result;
|
|
|
import com.backendsys.utils.response.ResultEnum;
|
|
@@ -20,7 +21,6 @@ import org.springframework.http.server.ServerHttpRequest;
|
|
|
import org.springframework.http.server.ServerHttpResponse;
|
|
|
import org.springframework.security.access.AccessDeniedException;
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
-import org.springframework.validation.FieldError;
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
@@ -84,6 +84,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
|
|
|
System.out.println("****** ConstraintViolationException.class: ******");
|
|
|
System.out.println(e);
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
|
|
|
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
|
|
|
Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
|
|
@@ -93,6 +95,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
msgList.add(cvl.getMessageTemplate());
|
|
|
}
|
|
|
System.out.println(msgList.get(0));
|
|
|
+ log.error(msgList.get(0));
|
|
|
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), msgList.get(0));
|
|
|
}
|
|
@@ -106,6 +109,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handlerMethodArgumentException(MethodArgumentNotValidException e){
|
|
|
System.out.println("****** MethodArgumentNotValidException.class: ******");
|
|
|
System.out.println(e.getMessage());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
e.getBindingResult().getAllErrors().get(0).getDefaultMessage(),
|
|
@@ -117,6 +122,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
|
|
System.out.println("****** MissingServletRequestParameterException.class: ******");
|
|
|
System.out.println(e.getMessage());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
"缺少参数 " + e.getParameterName() + " 或类型不匹配 (" + e.getParameterType() + ")",
|
|
@@ -133,6 +140,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleNumberFormatException(NumberFormatException e) {
|
|
|
System.out.println("****** NumberFormatException.class: ******");
|
|
|
System.out.println(e);
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -143,8 +152,9 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(CustomException.class)
|
|
|
public Result handleCustomException(CustomException e) {
|
|
|
System.out.println("****** CustomException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.info(e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(e.getErrorCode() != null ? e.getErrorCode() : ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage(), e.getErrorObject());
|
|
|
}
|
|
|
/**
|
|
@@ -154,6 +164,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public String handleCustomExceptionSimple(CustomExceptionSimple e) {
|
|
|
System.out.println("****** CustomExceptionSimple.class: ******");
|
|
|
System.out.println(e);
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
|
|
@@ -165,6 +177,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleTooManyResultsException(TooManyResultsException e) {
|
|
|
System.out.println("****** TooManyResultsException.class: ******");
|
|
|
System.out.println(e);
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.SERVICE_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -175,7 +189,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleLoginException(LoginException e) {
|
|
|
System.out.println("****** LoginException.class: ******");
|
|
|
System.out.println(e);
|
|
|
- // [AUTH_ERROR: 授权失败]
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.AUTH_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -186,7 +201,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleAccessDeniedException(AccessDeniedException e) {
|
|
|
System.out.println("****** AccessDeniedException.class: ******");
|
|
|
System.out.println(e);
|
|
|
- // [AUTH_ROLE_ERROR: 无访问权限]
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.AUTH_ROLE_ERROR.getCode(), ResultEnum.AUTH_ROLE_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
@@ -198,6 +214,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleNoHandlerFoundException(NoHandlerFoundException e) {
|
|
|
System.out.println("****** NoHandlerFoundException.class: ******");
|
|
|
System.out.println(e);
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -209,12 +227,16 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
System.out.println("****** MyBatisSystemException.class: ******");
|
|
|
System.out.println(e.getMessage());
|
|
|
System.out.println(bindingResult.toString());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(bindingResult.toString());
|
|
|
+
|
|
|
Throwable cause = e.getCause();
|
|
|
if (cause instanceof PersistenceException) {
|
|
|
Throwable sqlException = cause.getCause();
|
|
|
if (sqlException != null ) {
|
|
|
if (sqlException.getMessage() == "argument type mismatch") {
|
|
|
System.out.println(sqlException.getMessage() + ", 参数类型不匹配");
|
|
|
+ log.error(sqlException.getMessage() + ", 参数类型不匹配");
|
|
|
}
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), ResultEnum.INTERNAL_ERROR.getMessage(), sqlException.getMessage());
|
|
|
} else {
|
|
@@ -254,6 +276,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
System.out.println("****** RedisConnectionFailureException.class: ******");
|
|
|
System.out.println(e);
|
|
|
System.out.println(e.getMessage());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.REDIS_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -265,8 +289,9 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
System.out.println("****** HttpMessageNotReadableException.class: ******");
|
|
|
System.out.println(e);
|
|
|
System.out.println(e.getMessage());
|
|
|
- String message = (String) e.getMessage();
|
|
|
- return Result.error(ResultEnum.HTTP_BODY_EMPTY.getCode(), message != null ? message : ResultEnum.HTTP_BODY_EMPTY.getMessage());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ return Result.error(ResultEnum.HTTP_BODY_EMPTY.getCode(), e.getMessage() != null ? e.getMessage() : ResultEnum.HTTP_BODY_EMPTY.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -277,6 +302,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
System.out.println("****** HttpRequestMethodNotSupportedException.class: ******");
|
|
|
System.out.println(e);
|
|
|
System.out.println(e.getMessage());
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.HTTP_METHOD_ERROR.getCode(), ResultEnum.HTTP_METHOD_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
@@ -291,75 +318,32 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
System.out.println("****** Exception.class: ******");
|
|
|
System.out.println(e);
|
|
|
System.out.println(e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
-
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// @ModelAttribute
|
|
|
-// public void addAttributes(Model model) {
|
|
|
-// model.addAttribute("error", "error test");
|
|
|
-// }
|
|
|
-//
|
|
|
-// @ExceptionHandler(RuntimeException.class)
|
|
|
-// public ModelAndView modelAndView(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
|
|
-// ModelAndView modelAndView = new ModelAndView("login");
|
|
|
-// if (ex instanceof NullPointerException) {
|
|
|
-// modelAndView.addObject("error", "自定义error" + ex.toString());
|
|
|
-// }
|
|
|
-// return modelAndView;
|
|
|
-// }
|
|
|
-
|
|
|
// 非法参数
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
public Result handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
System.out.println("****** IllegalArgumentException.class: ******");
|
|
|
+ log.error(e.getMessage());
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), ResultEnum.PARAMETER_EXCEPTION.getMessage(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
|
|
|
-// @ExceptionHandler(UnknownHostException.class)
|
|
|
-// public Result handleUnknownHostException(UnknownHostException e) {
|
|
|
-// System.out.println("****** UnknownHostException.class: ******");
|
|
|
-// return Result.error(ResultEnum.REMOTE_EXCEPTION.getCode(), ResultEnum.REMOTE_EXCEPTION.getMessage(), e.getMessage());
|
|
|
-// }
|
|
|
-// @ExceptionHandler(IOException.class)
|
|
|
-// public Result handleIOException(IOException e) {
|
|
|
-// System.out.println("****** IOException.class: ******");
|
|
|
-// return Result.error(ResultEnum.REMOTE_EXCEPTION.getCode(), ResultEnum.REMOTE_EXCEPTION.getMessage(), e.getMessage());
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- // https://blog.csdn.net/wzwsq/article/details/117993705
|
|
|
- // NumberFormatException 继承 IllegalArgumentException 继承 RuntimeException
|
|
|
-
|
|
|
-
|
|
|
- //@ExceptionHandler(NullPointerException.class)
|
|
|
- //public Result handleNullPointerException(NullPointerException e) {
|
|
|
- // System.out.println("****** NullPointerException.class: ******");
|
|
|
- // return Result.error(ResultEnum.NULL_POINTER.getCode(), ResultEnum.NULL_POINTER.getMessage(), e.getMessage());
|
|
|
- //}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
// 优先级太高,会覆盖其他异常
|
|
|
// @ExceptionHandler(RuntimeException.class)
|
|
|
// public Result handleRuntimeException(RuntimeException e) {
|
|
|
// System.out.println("****** RuntimeException.class: ******");
|
|
|
// System.out.println(e);
|
|
|
//// System.out.println(e.getMessage());
|
|
|
-//// System.out.println(e.getClass().getTypeName());
|
|
|
//// if (e.getClass().getTypeName() == "java.lang.NullPointerException") {
|
|
|
//// return Result.error(ResultEnum.NULL_POINTER.getCode(), e.getMessage());
|
|
|
//// }
|
|
|
// return Result.error(ResultEnum.SERVICE_EXCEPTION.getCode(), e.getMessage());
|
|
|
// }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|