From e588e8de6c4b3ecd8f8df329c462259007524803 Mon Sep 17 00:00:00 2001 From: whh <2846845743@qq.com> Date: Fri, 27 Sep 2024 10:38:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E5=92=8C=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=9B=B8=E5=85=B3=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/haihang/config/WebExceptionAdvice.java | 17 +++++++++++++++++ .../com/haihang/utils/LoginInterceptor.java | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 document/src/main/java/com/haihang/config/WebExceptionAdvice.java diff --git a/document/src/main/java/com/haihang/config/WebExceptionAdvice.java b/document/src/main/java/com/haihang/config/WebExceptionAdvice.java new file mode 100644 index 0000000..26ae29e --- /dev/null +++ b/document/src/main/java/com/haihang/config/WebExceptionAdvice.java @@ -0,0 +1,17 @@ +package com.haihang.config; + +import com.haihang.utils.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +@RestControllerAdvice +public class WebExceptionAdvice { + + @ExceptionHandler(RuntimeException.class) + public Result handleRuntimeException(RuntimeException e) { + log.error(e.toString(), e); + return Result.fail("服务器异常:"+e.getMessage()); + } +} diff --git a/document/src/main/java/com/haihang/utils/LoginInterceptor.java b/document/src/main/java/com/haihang/utils/LoginInterceptor.java index c75361b..c60d387 100644 --- a/document/src/main/java/com/haihang/utils/LoginInterceptor.java +++ b/document/src/main/java/com/haihang/utils/LoginInterceptor.java @@ -38,7 +38,7 @@ public class LoginInterceptor implements HandlerInterceptor { String token = request.getHeader("authorization"); if (StringUtils.isEmpty(token)) { //前端没传 - throw new Exception("前端没有携带token!"); + throw new RuntimeException("前端没有携带token!"); } //从redis中获取用户 @@ -47,7 +47,7 @@ public class LoginInterceptor implements HandlerInterceptor { .entries(LOGIN_USER_KEY + token); //前端的这个token没有值 if (userMap.isEmpty()) { - throw new Exception("无效token!!"); + throw new RuntimeException("无效token!!"); } // //hash转UserDTO存入ThreadLocal