|
@@ -2,7 +2,7 @@ package work.baiyun.chronicdiseaseapp.exception;
|
|
|
|
|
|
|
|
import jakarta.validation.*;
|
|
import jakarta.validation.*;
|
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
|
-import work.baiyun.chronicdiseaseapp.enums.ExceptionResultCode;
|
|
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.ErrorCode;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.BindException;
|
|
@@ -31,13 +31,14 @@ public class CustomExceptionHandler {
|
|
|
*/
|
|
*/
|
|
|
@ExceptionHandler(value = BindException.class)
|
|
@ExceptionHandler(value = BindException.class)
|
|
|
public R<Void> errorHandler(BindException ex) {
|
|
public R<Void> errorHandler(BindException ex) {
|
|
|
|
|
+ log.error("BindException occurred", ex);
|
|
|
BindingResult result = ex.getBindingResult();
|
|
BindingResult result = ex.getBindingResult();
|
|
|
StringBuilder errorMsg = new StringBuilder();
|
|
StringBuilder errorMsg = new StringBuilder();
|
|
|
for (ObjectError error : result.getAllErrors()) {
|
|
for (ObjectError error : result.getAllErrors()) {
|
|
|
errorMsg.append(error.getDefaultMessage()).append(", ");
|
|
errorMsg.append(error.getDefaultMessage()).append(", ");
|
|
|
}
|
|
}
|
|
|
errorMsg.delete(errorMsg.length() - 2, errorMsg.length());
|
|
errorMsg.delete(errorMsg.length() - 2, errorMsg.length());
|
|
|
- return R.fail(ExceptionResultCode.VALID_EXCEPTION.getCode(), errorMsg.toString());
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.PARAMETER_ERROR.getCode(), errorMsg.toString());
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 参数校验异常
|
|
* 参数校验异常
|
|
@@ -47,12 +48,31 @@ public class CustomExceptionHandler {
|
|
|
*/
|
|
*/
|
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
|
public R<Void> validationErrorHandler(ConstraintViolationException ex) {
|
|
public R<Void> validationErrorHandler(ConstraintViolationException ex) {
|
|
|
|
|
+ log.error("ConstraintViolationException occurred", ex);
|
|
|
List<String> errorInformation = ex.getConstraintViolations()
|
|
List<String> errorInformation = ex.getConstraintViolations()
|
|
|
.stream()
|
|
.stream()
|
|
|
.map(ConstraintViolation::getMessage)
|
|
.map(ConstraintViolation::getMessage)
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
String message = errorInformation.toString().substring(1, errorInformation.toString().length() - 1);
|
|
String message = errorInformation.toString().substring(1, errorInformation.toString().length() - 1);
|
|
|
- return R.fail(ExceptionResultCode.VALID_EXCEPTION.getCode(), message);
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.PARAMETER_ERROR.getCode(), message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 非受检的参数非法异常
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(IllegalArgumentException.class)
|
|
|
|
|
+ public R<Void> illegalArgumentHandler(IllegalArgumentException ex) {
|
|
|
|
|
+ log.warn("IllegalArgumentException: {}", ex.getMessage());
|
|
|
|
|
+ return R.fail(ErrorCode.PARAMETER_ERROR.getCode(), ErrorCode.PARAMETER_ERROR.getMessage() + ": " + ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 非法状态异常 -> 映射为系统异常(不可预期),记录日志
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(IllegalStateException.class)
|
|
|
|
|
+ public R<Void> illegalStateHandler(IllegalStateException ex) {
|
|
|
|
|
+ log.error("IllegalStateException", ex);
|
|
|
|
|
+ return R.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage());
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 自定义异常
|
|
* 自定义异常
|
|
@@ -63,7 +83,13 @@ public class CustomExceptionHandler {
|
|
|
@ExceptionHandler(value = CustomException.class)
|
|
@ExceptionHandler(value = CustomException.class)
|
|
|
public R<Void> customExceptionHandler(CustomException customException) {
|
|
public R<Void> customExceptionHandler(CustomException customException) {
|
|
|
String message = customException.getMessage();
|
|
String message = customException.getMessage();
|
|
|
- return R.fail(ExceptionResultCode.EXCEPTION.getCode(), message);
|
|
|
|
|
|
|
+ int code = customException.getCode();
|
|
|
|
|
+ if (code > 0) {
|
|
|
|
|
+ return R.fail(code, message);
|
|
|
|
|
+ }
|
|
|
|
|
+ // fallback to common system error
|
|
|
|
|
+ log.error("CustomException occurred", customException);
|
|
|
|
|
+ return R.fail(ErrorCode.SYSTEM_ERROR.getCode(), message);
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
|
* 未知异常
|
|
* 未知异常
|
|
@@ -74,6 +100,6 @@ public class CustomExceptionHandler {
|
|
|
@ExceptionHandler(value = Exception.class)
|
|
@ExceptionHandler(value = Exception.class)
|
|
|
public R<Void> exceptionHandler(Exception exception) {
|
|
public R<Void> exceptionHandler(Exception exception) {
|
|
|
log.error("Unhandled exception", exception);
|
|
log.error("Unhandled exception", exception);
|
|
|
- return R.fail(ExceptionResultCode.EXCEPTION.getCode(), ExceptionResultCode.EXCEPTION.getMsg());
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|