|
@@ -4,6 +4,7 @@ import cn.hutool.core.convert.Convert;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.backendsys.utils.response.Result;
|
|
|
import com.backendsys.utils.response.ResultEnum;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.validation.ConstraintViolation;
|
|
|
import jakarta.validation.ConstraintViolationException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -26,6 +27,8 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
@@ -75,6 +78,37 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
return o;
|
|
|
}
|
|
|
|
|
|
+ // -- 自定义异常输出结构 ----------------------------------------------------
|
|
|
+ public static void printException(Exception e, Boolean isPrintStack) {
|
|
|
+
|
|
|
+ log.error("-------------------------------------------------------------------------------");
|
|
|
+ System.out.println("--------------------------------------------------");
|
|
|
+
|
|
|
+ // 获取请求的URL
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (attributes != null) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ if (request != null) {
|
|
|
+ String url = request.getRequestURL().toString();
|
|
|
+ log.error("Exception occurred in request: {}", url);
|
|
|
+ System.out.println("Exception occurred in request: " + url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录异常消息
|
|
|
+ log.error("Exception message: " + e.getMessage());
|
|
|
+ // 记录当前异常类的class名称
|
|
|
+ log.error("Exception class: {}", e.getClass().getName());
|
|
|
+ // 记录异常的详细信息
|
|
|
+ if (isPrintStack) {
|
|
|
+ log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static void printException(Exception e) {
|
|
|
+ printException(e, false);
|
|
|
+ }
|
|
|
+ // -----------------------------------------------------------------------
|
|
|
|
|
|
/**
|
|
|
* 异常 参数错误
|
|
@@ -83,9 +117,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleConstraintViolationException(ConstraintViolationException e) {
|
|
|
|
|
|
System.out.println("****** ConstraintViolationException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
|
|
|
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
|
|
|
Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
|
|
@@ -108,9 +140,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
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()));
|
|
|
+ printException(e);
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
e.getBindingResult().getAllErrors().get(0).getDefaultMessage(),
|
|
@@ -121,9 +151,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
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()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
"缺少参数 " + e.getParameterName() + " 或类型不匹配 (" + e.getParameterType() + ")",
|
|
@@ -139,9 +167,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(NumberFormatException.class)
|
|
|
public Result handleNumberFormatException(NumberFormatException e) {
|
|
|
System.out.println("****** NumberFormatException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -152,9 +178,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(CustomException.class)
|
|
|
public Result handleCustomException(CustomException e) {
|
|
|
System.out.println("****** CustomException.class: ******");
|
|
|
- e.printStackTrace();
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
return Result.error(e.getErrorCode() != null ? e.getErrorCode() : ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage(), e.getErrorObject());
|
|
|
}
|
|
|
/**
|
|
@@ -163,9 +187,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(CustomExceptionSimple.class)
|
|
|
public String handleCustomExceptionSimple(CustomExceptionSimple e) {
|
|
|
System.out.println("****** CustomExceptionSimple.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
|
|
@@ -176,9 +198,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(TooManyResultsException.class)
|
|
|
public Result handleTooManyResultsException(TooManyResultsException e) {
|
|
|
System.out.println("****** TooManyResultsException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.SERVICE_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -188,9 +208,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(LoginException.class)
|
|
|
public Result handleLoginException(LoginException e) {
|
|
|
System.out.println("****** LoginException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.AUTH_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -200,9 +218,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(AccessDeniedException.class)
|
|
|
public Result handleAccessDeniedException(AccessDeniedException e) {
|
|
|
System.out.println("****** AccessDeniedException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.AUTH_ROLE_ERROR.getCode(), ResultEnum.AUTH_ROLE_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
@@ -213,9 +229,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(NoHandlerFoundException.class)
|
|
|
public Result handleNoHandlerFoundException(NoHandlerFoundException e) {
|
|
|
System.out.println("****** NoHandlerFoundException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -225,10 +239,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MyBatisSystemException.class)
|
|
|
public Result handleMyBatisSystemException(MyBatisSystemException e, BindingResult bindingResult) { // , BindingResult bindingResult
|
|
|
System.out.println("****** MyBatisSystemException.class: ******");
|
|
|
- System.out.println(e.getMessage());
|
|
|
- System.out.println(bindingResult.toString());
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(bindingResult.toString());
|
|
|
+ printException(e, true);
|
|
|
|
|
|
Throwable cause = e.getCause();
|
|
|
if (cause instanceof PersistenceException) {
|
|
@@ -246,17 +257,6 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
}
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
-// @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
|
|
|
-// public Result handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) { // , BindingResult bindingResult
|
|
|
-// System.out.println("****** SQLIntegrityConstraintViolationException.class: ******");
|
|
|
-// System.out.println(e);
|
|
|
-// return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
-// }
|
|
|
-// @ExceptionHandler(ReflectionException.class)
|
|
|
-// public Result handleReflectionException(ReflectionException e) {
|
|
|
-// System.out.println("****** ReflectionException.class: ******");
|
|
|
-// return Result.error(ResultEnum.REFLECTION_EXCEPTION.getCode(), ResultEnum.REFLECTION_EXCEPTION.getMessage(), e.getMessage());
|
|
|
-// }
|
|
|
|
|
|
/**
|
|
|
* MySQL 数据库 唯一值异常
|
|
@@ -264,6 +264,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(DuplicateKeyException.class)
|
|
|
public Result handleDuplicateKeyException(DuplicateKeyException e) {
|
|
|
System.out.println("****** DuplicateKeyException.class: ******");
|
|
|
+ printException(e, true);
|
|
|
DuplicateKeyExceptionHandler handler = new DuplicateKeyExceptionHandler();
|
|
|
return handler.handleDuplicateKeyException(e);
|
|
|
}
|
|
@@ -274,10 +275,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(RedisConnectionFailureException.class)
|
|
|
public Result handleRedisConnectionFailureException(Exception e) {
|
|
|
System.out.println("****** RedisConnectionFailureException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(e.getMessage());
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.REDIS_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -287,10 +285,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
|
|
System.out.println("****** HttpMessageNotReadableException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(e.getMessage());
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
return Result.error(ResultEnum.HTTP_BODY_EMPTY.getCode(), e.getMessage() != null ? e.getMessage() : ResultEnum.HTTP_BODY_EMPTY.getMessage());
|
|
|
}
|
|
|
|
|
@@ -300,15 +295,10 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
|
|
System.out.println("****** HttpRequestMethodNotSupportedException.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(e.getMessage());
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
return Result.error(ResultEnum.HTTP_METHOD_ERROR.getCode(), ResultEnum.HTTP_METHOD_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 常见异常:
|
|
|
* - 请求方法 Post/Get 不支持
|
|
@@ -316,10 +306,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
public Result handleException(Exception e) { // , BindingResult bindingResult
|
|
|
System.out.println("****** Exception.class: ******");
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(e.getMessage());
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e, true);
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -327,12 +314,10 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
public Result handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
System.out.println("****** IllegalArgumentException.class: ******");
|
|
|
- log.error(e.getMessage());
|
|
|
- log.error(Convert.toStr(e.getStackTrace()));
|
|
|
+ printException(e);
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), ResultEnum.PARAMETER_EXCEPTION.getMessage(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// 优先级太高,会覆盖其他异常
|
|
|
// @ExceptionHandler(RuntimeException.class)
|
|
|
// public Result handleRuntimeException(RuntimeException e) {
|