From c9cb410819a554c1430f074baa1ea1924f41d0f2 Mon Sep 17 00:00:00 2001 From: KilLze Date: Mon, 29 Dec 2025 19:29:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/GlobalExceptionHandler.java | 109 ++++++++++++++++++ .../dating/interceptor/TokenInterceptor.java | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java diff --git a/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java b/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..4e1993d --- /dev/null +++ b/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java @@ -0,0 +1,109 @@ +package com.bao.dating.handler; + +import com.bao.dating.common.Result; +import com.bao.dating.common.ResultCode; +import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.http.HttpStatus; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +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.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.servlet.NoHandlerFoundException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 全局异常处理器 + * 统一处理控制器层抛出的异常 + * @author KilLze + */ +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + /** + * 处理参数验证失败异常 + */ + @ExceptionHandler(MethodArgumentNotValidException.class) + public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { + log.error("参数验证失败: {}", e.getMessage()); + return Result.error(ResultCode.PARAM_ERROR, e.getBindingResult().getFieldError().getDefaultMessage()); + } + + /** + * 处理请求参数缺失异常 + */ + @ExceptionHandler(MissingServletRequestParameterException.class) + public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { + log.error("请求参数缺失: 参数名={}, 参数类型={}", e.getParameterName(), e.getParameterType()); + return Result.error(ResultCode.PARAM_ERROR, "缺少必需的请求参数: " + e.getParameterName()); + } + + /** + * 处理请求参数类型不匹配异常 + */ + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public Result handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) { + log.error("请求参数类型不匹配: {}", e.getMessage()); + return Result.error(ResultCode.PARAM_ERROR, "请求参数类型错误: " + e.getName()); + } + + /** + * 处理请求体缺失或格式错误异常 + */ + @ExceptionHandler(HttpMessageNotReadableException.class) + public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { + log.error("请求体格式错误: {}", e.getMessage()); + return Result.error(ResultCode.PARAM_ERROR, "请求体格式错误或缺失"); + } + + /** + * 处理不支持的HTTP请求方法异常 + */ + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { + log.error("不支持的HTTP请求方法: {}", e.getMethod()); + return Result.error(ResultCode.PARAM_ERROR, "不支持的请求方法: " + e.getMethod()); + } + + /** + * 处理404异常 + */ + @ExceptionHandler(NoHandlerFoundException.class) + public Result handleNoHandlerFoundException(HttpServletRequest request, NoHandlerFoundException e) { + log.error("请求的接口不存在: {} {}", request.getMethod(), request.getRequestURI()); + return Result.error(ResultCode.NOT_FOUND, "请求的接口不存在"); + } + + /** + * 处理数据库唯一约束违反异常 + */ + @ExceptionHandler(DuplicateKeyException.class) + public Result handleDuplicateKeyException(DuplicateKeyException e) { + log.error("数据库唯一约束违反: {}", e.getMessage()); + return Result.error(ResultCode.FAIL, "数据已存在,操作失败"); + } + + /** + * 处理运行时异常 + */ + @ExceptionHandler(RuntimeException.class) + public Result handleRuntimeException(RuntimeException e) { + log.error("运行时异常: ", e); + return Result.error(ResultCode.SYSTEM_ERROR, e.getMessage()); + } + + /** + * 处理通用异常 + */ + @ExceptionHandler(Exception.class) + public Result handleException(HttpServletRequest request, Exception e) { + log.error("系统异常 [{} {}]: ", request.getMethod(), request.getRequestURI(), e); + return Result.error(ResultCode.SYSTEM_ERROR, "系统内部错误"); + } +} \ No newline at end of file diff --git a/src/main/java/com/bao/dating/interceptor/TokenInterceptor.java b/src/main/java/com/bao/dating/interceptor/TokenInterceptor.java index 8dee76c..e520ce8 100644 --- a/src/main/java/com/bao/dating/interceptor/TokenInterceptor.java +++ b/src/main/java/com/bao/dating/interceptor/TokenInterceptor.java @@ -43,7 +43,7 @@ public class TokenInterceptor implements HandlerInterceptor { // 验证 token 是否有效(包括是否过期) if (!JwtUtil.validateToken(token)) { - log.error("Token 无效或已过期"); + log.error("Token无效或已过期"); response.setStatus(401); return false; }