package com.webmanage.common; 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 org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.stream.Collectors; /** * 全局异常处理器 * * @author webmanage * @date 2024-08-07 */ @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { /** * 处理参数校验异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { String message = e.getBindingResult().getFieldErrors().stream() .map(FieldError::getDefaultMessage) .collect(Collectors.joining(", ")); log.warn("参数校验失败: {}", message); return Result.error(400, "参数校验失败: " + message); } /** * 处理绑定异常 */ @ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleBindException(BindException e) { String message = e.getBindingResult().getFieldErrors().stream() .map(FieldError::getDefaultMessage) .collect(Collectors.joining(", ")); log.warn("参数绑定失败: {}", message); return Result.error(400, "参数绑定失败: " + message); } /** * 处理约束违反异常 */ @ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleConstraintViolationException(ConstraintViolationException e) { String message = e.getConstraintViolations().stream() .map(ConstraintViolation::getMessage) .collect(Collectors.joining(", ")); log.warn("约束校验失败: {}", message); return Result.error(400, "约束校验失败: " + message); } /** * 处理参数类型不匹配异常 */ @ExceptionHandler(MethodArgumentTypeMismatchException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) { log.warn("参数类型不匹配: {}", e.getMessage()); return Result.error(400, "参数类型不匹配: " + e.getName()); } /** * 处理业务异常 */ @ExceptionHandler(BusinessException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleBusinessException(BusinessException e) { log.warn("业务异常: {}", e.getMessage()); return Result.error(e.getCode(), e.getMessage()); } /** * 处理文件上传异常 */ @ExceptionHandler(FileUploadException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Result handleFileUploadException(FileUploadException e) { log.warn("文件上传异常: {}", e.getMessage()); return Result.error(e.getCode(), e.getMessage()); } /** * 处理通用运行时异常 */ @ExceptionHandler(RuntimeException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result handleRuntimeException(RuntimeException e) { log.error("运行时异常: ", e); return Result.error(500, "系统内部错误: " + e.getMessage()); } /** * 处理通用异常 */ @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Result handleException(Exception e) { log.error("系统异常: ", e); return Result.error(500, "系统异常,请联系管理员"); } }