fix: 修复当登录的TOKEN认证失败后,抛出的异常中无法被识别到导致前端无法捕获此类异常数据导致无法正确返回页面

This commit is contained in:
zhangyanduan
2022-06-27 14:57:07 +08:00
parent b867799859
commit 7584a3cc51
3 changed files with 16 additions and 4 deletions
@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
* <p> * <p>
* Created at 2020/8/6 5:03 下午 * Created at 2020/8/6 5:03 下午
*/ */
@RestControllerAdvice //@RestControllerAdvice
public class SysRuntimeExceptionHandler { public class SysRuntimeExceptionHandler {
@ExceptionHandler(TokenRuntimeException.class) @ExceptionHandler(TokenRuntimeException.class)
@@ -12,7 +12,7 @@ import lombok.Data;
* Created at 2020/8/6 4:58 下午 * Created at 2020/8/6 4:58 下午
*/ */
@Data @Data
public class TokenRuntimeException extends RuntimeException{ public class TokenRuntimeException extends Exception{
private String code = "401"; private String code = "401";
private String msg; private String msg;
@@ -13,11 +13,23 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.HashSet;
import java.util.Set;
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
@Order(value=4) @Order(value=4)
public class AdcDaBaseExceptionAdvice { public class AdcDaBaseExceptionAdvice {
private static Set<String> errMsg = new HashSet<>();
static {
errMsg.add("token已过期");
errMsg.add("未登录");
errMsg.add("token解析错误");
errMsg.add("员工不存在");
}
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ExceptionHandler(AdcDaBaseException.class) @ExceptionHandler(AdcDaBaseException.class)
@@ -34,8 +46,8 @@ public class AdcDaBaseExceptionAdvice {
log.error(exception.getMessage(), exception); log.error(exception.getMessage(), exception);
// TODO 在数据库中记录程序异常,这个地方的异常是未处理的异常,需要管理员查看并进行处理以防重复出现 // TODO 在数据库中记录程序异常,这个地方的异常是未处理的异常,需要管理员查看并进行处理以防重复出现
if(exception.getStackTrace() != null){ if(exception.getStackTrace() != null){
if("token已过期".equals(exception.getMessage())){ if(errMsg.contains(exception.getMessage())){
return Result.error(ResponseMessageCodeEnum.ERROR_TOKEN.getCode(), "token已过期,请重新登录"); return Result.error(ResponseMessageCodeEnum.ERROR_TOKEN.getCode(), "token无效,请登录");
} }
} }
return Result.error(ResponseMessageCodeEnum.ERROR.getCode(), "程序异常,请重试。如果重复出现请联系管理员处理!"); return Result.error(ResponseMessageCodeEnum.ERROR.getCode(), "程序异常,请重试。如果重复出现请联系管理员处理!");