|
|
@@ -3,6 +3,8 @@ package work.baiyun.chronicdiseaseapp.exception;
|
|
|
import jakarta.validation.*;
|
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
|
import work.baiyun.chronicdiseaseapp.enums.ExceptionResultCode;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.validation.BindException;
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
@@ -20,6 +22,7 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@RestControllerAdvice
|
|
|
public class CustomExceptionHandler {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(CustomExceptionHandler.class);
|
|
|
/**
|
|
|
* 参数校验异常
|
|
|
*
|
|
|
@@ -27,7 +30,7 @@ public class CustomExceptionHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
@ExceptionHandler(value = BindException.class)
|
|
|
- public R errorHandler(BindException ex) {
|
|
|
+ public R<Void> errorHandler(BindException ex) {
|
|
|
BindingResult result = ex.getBindingResult();
|
|
|
StringBuilder errorMsg = new StringBuilder();
|
|
|
for (ObjectError error : result.getAllErrors()) {
|
|
|
@@ -43,7 +46,7 @@ public class CustomExceptionHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
|
- public R validationErrorHandler(ConstraintViolationException ex) {
|
|
|
+ public R<Void> validationErrorHandler(ConstraintViolationException ex) {
|
|
|
List<String> errorInformation = ex.getConstraintViolations()
|
|
|
.stream()
|
|
|
.map(ConstraintViolation::getMessage)
|
|
|
@@ -58,7 +61,7 @@ public class CustomExceptionHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
@ExceptionHandler(value = CustomException.class)
|
|
|
- public R customExceptionHandler(CustomException customException) {
|
|
|
+ public R<Void> customExceptionHandler(CustomException customException) {
|
|
|
String message = customException.getMessage();
|
|
|
return R.fail(ExceptionResultCode.EXCEPTION.getCode(), message);
|
|
|
}
|
|
|
@@ -69,8 +72,8 @@ public class CustomExceptionHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
@ExceptionHandler(value = Exception.class)
|
|
|
- public R exceptionHandler(Exception exception) {
|
|
|
- exception.printStackTrace();
|
|
|
+ public R<Void> exceptionHandler(Exception exception) {
|
|
|
+ log.error("Unhandled exception", exception);
|
|
|
return R.fail(ExceptionResultCode.EXCEPTION.getCode(), ExceptionResultCode.EXCEPTION.getMsg());
|
|
|
}
|
|
|
}
|