2025-04-01 11:48:31 +08:00
|
|
|
|
package com.greenorange.promotion.exception;
|
|
|
|
|
|
|
|
|
|
import com.greenorange.promotion.common.BaseResponse;
|
|
|
|
|
import com.greenorange.promotion.common.ErrorCode;
|
|
|
|
|
import com.greenorange.promotion.common.ResultUtils;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Hidden;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-04-29 11:49:04 +08:00
|
|
|
|
import net.sf.jsqlparser.util.validation.ValidationError;
|
2025-05-03 10:20:50 +08:00
|
|
|
|
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
2025-04-30 10:18:18 +08:00
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2025-04-01 11:48:31 +08:00
|
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
2025-04-29 11:49:04 +08:00
|
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
|
|
import org.springframework.validation.FieldError;
|
2025-04-28 12:34:22 +08:00
|
|
|
|
import org.springframework.validation.ObjectError;
|
2025-04-01 11:48:31 +08:00
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
2025-04-30 10:18:18 +08:00
|
|
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
2025-04-01 11:48:31 +08:00
|
|
|
|
|
2025-05-03 10:20:50 +08:00
|
|
|
|
import java.sql.SQLException;
|
2025-04-29 11:49:04 +08:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2025-04-01 11:48:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 全局异常处理器
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Hidden
|
|
|
|
|
@RestControllerAdvice
|
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
|
|
|
2025-04-30 13:34:09 +08:00
|
|
|
|
// 处理参数类型不匹配的异常(GET请求)
|
|
|
|
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
|
|
public BaseResponse<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
|
|
|
|
|
return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求参数类型不匹配: " + ex.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理消息体解析失败的异常(POST请求)
|
|
|
|
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
|
|
public BaseResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
|
|
|
|
log.error("HttpMessageNotReadableException", e);
|
|
|
|
|
return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求体不能为空或格式无效");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-01 11:48:31 +08:00
|
|
|
|
// 处理参数绑定失败的异常
|
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
2025-06-24 09:18:01 +08:00
|
|
|
|
// StringBuilder errors = new StringBuilder();
|
|
|
|
|
// // 按字段名排序,确保每次返回的顺序一致
|
|
|
|
|
// e.getBindingResult().getFieldErrors().stream()
|
|
|
|
|
// .sorted(Comparator.comparing(FieldError::getField)) // 按字段名排序
|
|
|
|
|
// .forEach(fieldError -> errors.append("参数: ")
|
|
|
|
|
// .append(fieldError.getField())
|
|
|
|
|
// .append(" | 错误: ")
|
|
|
|
|
// .append(fieldError.getDefaultMessage())
|
|
|
|
|
// .append("; "));
|
|
|
|
|
// return ResultUtils.error(ErrorCode.PARAMS_ERROR, errors.toString());
|
|
|
|
|
|
|
|
|
|
// 从所有 FieldError 里,排序后取第一个
|
|
|
|
|
FieldError firstError = e.getBindingResult()
|
|
|
|
|
.getFieldErrors().stream().min(Comparator.comparing(FieldError::getField))
|
|
|
|
|
.orElse(null);
|
|
|
|
|
|
|
|
|
|
// 直接取它的 defaultMessage,即注解里配置的 message
|
|
|
|
|
String msg = (firstError != null)
|
|
|
|
|
? firstError.getDefaultMessage()
|
|
|
|
|
: "参数校验失败";
|
|
|
|
|
|
|
|
|
|
// 返回时只带 msg,不再拼前缀或字段名
|
|
|
|
|
return ResultUtils.error(ErrorCode.PARAMS_ERROR, msg);
|
2025-04-01 11:48:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 11:49:04 +08:00
|
|
|
|
|
2025-04-29 21:37:00 +08:00
|
|
|
|
// 处理业务异常
|
2025-04-01 11:48:31 +08:00
|
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
|
|
|
public BaseResponse<?> businessExceptionHandler(BusinessException e) {
|
|
|
|
|
log.error("BusinessException", e);
|
|
|
|
|
return ResultUtils.error(e.getCode(), e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-05-03 10:20:50 +08:00
|
|
|
|
// 处理 SQL 语法错误
|
|
|
|
|
@ExceptionHandler(SQLException.class)
|
|
|
|
|
public BaseResponse<?> handleSQLException(SQLException ex) {
|
|
|
|
|
// 记录详细的 SQL 错误信息
|
|
|
|
|
log.error("SQL 错误: " + ex.getMessage());
|
|
|
|
|
return ResultUtils.error(ErrorCode.DATABASE_ERROR, "SQL语法错误,详细信息:" + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-01 11:48:31 +08:00
|
|
|
|
|
|
|
|
|
}
|