|
@@ -9,6 +9,9 @@ import com.aizuda.common.toolkit.ThrowableUtils;
|
|
import com.aizuda.core.api.ApiResult;
|
|
import com.aizuda.core.api.ApiResult;
|
|
import com.aizuda.core.api.IErrorCode;
|
|
import com.aizuda.core.api.IErrorCode;
|
|
import com.aizuda.core.exception.ApiException;
|
|
import com.aizuda.core.exception.ApiException;
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
+import jakarta.validation.ConstraintViolationException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -23,9 +26,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
import org.springframework.web.util.NestedServletException;
|
|
import org.springframework.web.util.NestedServletException;
|
|
|
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
-import jakarta.validation.ConstraintViolationException;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -51,7 +51,6 @@ public class ServiceExceptionHandler {
|
|
*
|
|
*
|
|
* @param request {@link HttpServletRequest}
|
|
* @param request {@link HttpServletRequest}
|
|
* @param e {@link MethodArgumentNotValidException}
|
|
* @param e {@link MethodArgumentNotValidException}
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
@ExceptionHandler({MethodArgumentNotValidException.class})
|
|
@ExceptionHandler({MethodArgumentNotValidException.class})
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@@ -65,7 +64,6 @@ public class ServiceExceptionHandler {
|
|
*
|
|
*
|
|
* @param request {@link HttpServletRequest}
|
|
* @param request {@link HttpServletRequest}
|
|
* @param e {@link BindException}
|
|
* @param e {@link BindException}
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
@ExceptionHandler({BindException.class})
|
|
@ExceptionHandler({BindException.class})
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@@ -78,9 +76,8 @@ public class ServiceExceptionHandler {
|
|
* 验证异常处理 - @Validated加在 controller 类上,
|
|
* 验证异常处理 - @Validated加在 controller 类上,
|
|
* 且在参数列表中直接指定constraints时触发
|
|
* 且在参数列表中直接指定constraints时触发
|
|
*
|
|
*
|
|
- * @param request
|
|
|
|
- * @param ex
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param request HttpServletRequest
|
|
|
|
+ * @param ex ConstraintViolationException
|
|
*/
|
|
*/
|
|
@ExceptionHandler({ConstraintViolationException.class})
|
|
@ExceptionHandler({ConstraintViolationException.class})
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@@ -92,8 +89,7 @@ public class ServiceExceptionHandler {
|
|
/**
|
|
/**
|
|
* 转换FieldError列表为错误提示信息
|
|
* 转换FieldError列表为错误提示信息
|
|
*
|
|
*
|
|
- * @param fieldErrors
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param fieldErrors List<FieldError>
|
|
*/
|
|
*/
|
|
private String convertFiledErrors(List<FieldError> fieldErrors) {
|
|
private String convertFiledErrors(List<FieldError> fieldErrors) {
|
|
return Optional.ofNullable(fieldErrors)
|
|
return Optional.ofNullable(fieldErrors)
|
|
@@ -107,18 +103,15 @@ public class ServiceExceptionHandler {
|
|
/**
|
|
/**
|
|
* 转换ConstraintViolationException 异常为错误提示信息
|
|
* 转换ConstraintViolationException 异常为错误提示信息
|
|
*
|
|
*
|
|
- * @param constraintViolationException
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param constraintViolationException ConstraintViolationException
|
|
*/
|
|
*/
|
|
private String convertConstraintViolations(ConstraintViolationException constraintViolationException) {
|
|
private String convertConstraintViolations(ConstraintViolationException constraintViolationException) {
|
|
return Optional.ofNullable(constraintViolationException.getConstraintViolations())
|
|
return Optional.ofNullable(constraintViolationException.getConstraintViolations())
|
|
.filter(constraintViolations -> this.enableValidationMessage)
|
|
.filter(constraintViolations -> this.enableValidationMessage)
|
|
.map(constraintViolations -> constraintViolations.stream().flatMap(constraintViolation -> {
|
|
.map(constraintViolations -> constraintViolations.stream().flatMap(constraintViolation -> {
|
|
String path = constraintViolation.getPropertyPath().toString();
|
|
String path = constraintViolation.getPropertyPath().toString();
|
|
- StringBuffer errorMessage = new StringBuffer();
|
|
|
|
- errorMessage.append(path.substring(path.lastIndexOf(".") + 1));
|
|
|
|
- errorMessage.append(" ").append(constraintViolation.getMessage());
|
|
|
|
- return Stream.of(errorMessage.toString());
|
|
|
|
|
|
+ return Stream.of(path.substring(path.lastIndexOf(".") + 1) +
|
|
|
|
+ " " + constraintViolation.getMessage());
|
|
}).collect(Collectors.joining(", "))
|
|
}).collect(Collectors.joining(", "))
|
|
).orElse(null);
|
|
).orElse(null);
|
|
}
|
|
}
|
|
@@ -128,7 +121,6 @@ public class ServiceExceptionHandler {
|
|
*
|
|
*
|
|
* @param e 异常类型
|
|
* @param e 异常类型
|
|
* @param resp 响应请求
|
|
* @param resp 响应请求
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
@ExceptionHandler(value = Throwable.class)
|
|
@ExceptionHandler(value = Throwable.class)
|
|
public ApiResult<Object> handleBadRequest(Throwable e, HttpServletResponse resp) {
|
|
public ApiResult<Object> handleBadRequest(Throwable e, HttpServletResponse resp) {
|
|
@@ -153,7 +145,7 @@ public class ServiceExceptionHandler {
|
|
return ApiResult.failed(e.getMessage());
|
|
return ApiResult.failed(e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
|
|
+ /*
|
|
* 系统内部异常,打印异常栈
|
|
* 系统内部异常,打印异常栈
|
|
*/
|
|
*/
|
|
log.error("Error: handleBadRequest StackTrace : {}", ThrowableUtils.getStackTrace(e));
|
|
log.error("Error: handleBadRequest StackTrace : {}", ThrowableUtils.getStackTrace(e));
|