|
@@ -4,6 +4,10 @@ import com.etotem.cfc.common.Result;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.dao.DataIntegrityViolationException;
|
|
|
|
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
|
|
+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.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.web.multipart.MultipartException;
|
|
import org.springframework.web.multipart.MultipartException;
|
|
@@ -18,6 +22,41 @@ public class GlobalExceptionHandler {
|
|
|
return Result.error(400, e.getMessage());
|
|
return Result.error(400, e.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** 业务状态流转异常:以 400 + 业务提示返回,不再裸抛 500 */
|
|
|
|
|
+ @ExceptionHandler(IllegalStateException.class)
|
|
|
|
|
+ public Result<Void> handleIllegalStateException(IllegalStateException e) {
|
|
|
|
|
+ return Result.error(400, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 请求体缺失/JSON 语法错误/类型不匹配 */
|
|
|
|
|
+ @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
|
|
+ public Result<Void> handleHttpMessageNotReadable(HttpMessageNotReadableException e) {
|
|
|
|
|
+ log.warn("请求体解析失败: {}", e.getMessage());
|
|
|
|
|
+ return Result.error(400, "请求参数格式错误,请检查请求体 JSON 或字段类型");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 缺少必填请求参数(@RequestParam 未传) */
|
|
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
|
|
+ public Result<Void> handleMissingParam(MissingServletRequestParameterException e) {
|
|
|
|
|
+ return Result.error(400, "缺少必填参数: " + e.getParameterName());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** @Valid 参数校验失败:返回首个字段错误提示 */
|
|
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
+ public Result<Void> handleMethodArgumentNotValid(MethodArgumentNotValidException e) {
|
|
|
|
|
+ if (e.getBindingResult() != null && e.getBindingResult().getFieldError() != null) {
|
|
|
|
|
+ return Result.error(400, e.getBindingResult().getFieldError().getDefaultMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.error(400, "参数校验失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 字段缺失/超长/非空约束等数据完整性问题:改为 400 业务提示 */
|
|
|
|
|
+ @ExceptionHandler(DataIntegrityViolationException.class)
|
|
|
|
|
+ public Result<Void> handleDataIntegrityViolation(DataIntegrityViolationException e, HttpServletRequest request) {
|
|
|
|
|
+ log.warn("数据完整性校验失败: uri={}, error={}", request.getRequestURI(), e.getMessage());
|
|
|
|
|
+ return Result.error(400, "数据不完整或格式错误:请检查必填字段与字段长度");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@ExceptionHandler(MultipartException.class)
|
|
@ExceptionHandler(MultipartException.class)
|
|
|
public Result<Void> handleMultipartException(MultipartException e, HttpServletRequest request) {
|
|
public Result<Void> handleMultipartException(MultipartException e, HttpServletRequest request) {
|
|
|
log.warn("非multipart请求访问文件接口: uri={}, error={}", request.getRequestURI(), e.getMessage());
|
|
log.warn("非multipart请求访问文件接口: uri={}, error={}", request.getRequestURI(), e.getMessage());
|