Files
qingcheng-houduan/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java
2025-04-30 13:34:09 +08:00

80 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
import net.sf.jsqlparser.util.validation.ValidationError;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* 全局异常处理器
*/
@Slf4j
@Hidden
@RestControllerAdvice
public class GlobalExceptionHandler {
// 处理参数类型不匹配的异常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, "请求体不能为空或格式无效");
}
// 处理参数绑定失败的异常
@ExceptionHandler(MethodArgumentNotValidException.class)
public BaseResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
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());
}
// 处理业务异常
@ExceptionHandler(BusinessException.class)
public BaseResponse<?> businessExceptionHandler(BusinessException e) {
log.error("BusinessException", e);
return ResultUtils.error(e.getCode(), e.getMessage());
}
// // 处理运行时异常
// @ExceptionHandler(RuntimeException.class)
// public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
// log.error("RuntimeException", e);
// return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
// }
}