97 lines
3.5 KiB
Java
97 lines
3.5 KiB
Java
package com.sunnyfarm.exception;
|
|
|
|
import com.sunnyfarm.common.Result;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
import javax.validation.ConstraintViolationException;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
@RestControllerAdvice
|
|
@Slf4j
|
|
public class GlobalExceptionHandler {
|
|
|
|
/**
|
|
* 处理业务异常
|
|
*/
|
|
@ExceptionHandler(BusinessException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result<String> handleBusinessException(BusinessException e) {
|
|
log.error("业务异常: {}", e.getMessage());
|
|
return Result.error(e.getMessage());
|
|
}
|
|
|
|
/**
|
|
* 处理参数校验异常
|
|
*/
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result<String> handleValidException(MethodArgumentNotValidException e) {
|
|
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (FieldError error : fieldErrors) {
|
|
sb.append(error.getField()).append(": ").append(error.getDefaultMessage()).append("; ");
|
|
}
|
|
log.error("参数校验异常: {}", sb.toString());
|
|
return Result.error("参数校验失败: " + sb.toString());
|
|
}
|
|
|
|
/**
|
|
* 处理绑定异常
|
|
*/
|
|
@ExceptionHandler(BindException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result<String> handleBindException(BindException e) {
|
|
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (FieldError error : fieldErrors) {
|
|
sb.append(error.getField()).append(": ").append(error.getDefaultMessage()).append("; ");
|
|
}
|
|
log.error("绑定异常: {}", sb.toString());
|
|
return Result.error("参数绑定失败: " + sb.toString());
|
|
}
|
|
|
|
/**
|
|
* 处理约束违反异常
|
|
*/
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result<String> handleConstraintViolationException(ConstraintViolationException e) {
|
|
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (ConstraintViolation<?> violation : violations) {
|
|
sb.append(violation.getMessage()).append("; ");
|
|
}
|
|
log.error("约束违反异常: {}", sb.toString());
|
|
return Result.error("参数约束违反: " + sb.toString());
|
|
}
|
|
|
|
/**
|
|
* 处理运行时异常
|
|
*/
|
|
@ExceptionHandler(RuntimeException.class)
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
public Result<String> handleRuntimeException(RuntimeException e) {
|
|
log.error("运行时异常", e);
|
|
return Result.error("系统异常,请稍后重试");
|
|
}
|
|
|
|
/**
|
|
* 处理其他异常
|
|
*/
|
|
@ExceptionHandler(Exception.class)
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
public Result<String> handleException(Exception e) {
|
|
log.error("系统异常", e);
|
|
return Result.error("系统异常,请稍后重试");
|
|
}
|
|
}
|