Skip to content

Commit

Permalink
[Optimize]optimize_some_code (#2198)
Browse files Browse the repository at this point in the history
* optimize_some_code

* optimize_some_code

* optimize_some_code
  • Loading branch information
zackyoungh authored Aug 11, 2023
1 parent 6fdf857 commit 7b38967
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

Expand All @@ -46,6 +47,7 @@
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;

/**
Expand All @@ -67,6 +69,17 @@ public Result<Void> busException(BusException e) {
return Result.failed(e.getMsg());
}

private static final Map<String, Status> ERR_CODE_MAPPING =
MapUtil.<String, Status>builder()
.put(NotLoginException.NOT_TOKEN, Status.NOT_TOKEN)
.put(NotLoginException.INVALID_TOKEN, Status.INVALID_TOKEN)
.put(NotLoginException.TOKEN_TIMEOUT, Status.EXPIRED_TOKEN)
.put(NotLoginException.BE_REPLACED, Status.BE_REPLACED)
.put(NotLoginException.KICK_OUT, Status.KICK_OUT)
.put(NotLoginException.TOKEN_FREEZE, Status.TOKEN_FREEZED)
.put(NotLoginException.NO_PREFIX, Status.NO_PREFIX)
.build();

@ExceptionHandler
public Result<Void> notLoginException(NotLoginException notLoginException) {
ServletRequestAttributes servletRequestAttributes =
Expand All @@ -75,23 +88,10 @@ public Result<Void> notLoginException(NotLoginException notLoginException) {
if (response != null) {
response.setStatus(CodeEnum.NOTLOGIN.getCode());
}
if (notLoginException.getType().equals(NotLoginException.NOT_TOKEN)) {
return Result.failed(Status.NOT_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.INVALID_TOKEN)) {
return Result.failed(Status.INVALID_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
return Result.failed(Status.EXPIRED_TOKEN);
} else if (notLoginException.getType().equals(NotLoginException.BE_REPLACED)) {
return Result.failed(Status.BE_REPLACED);
} else if (notLoginException.getType().equals(NotLoginException.KICK_OUT)) {
return Result.failed(Status.KICK_OUT);
} else if (notLoginException.getType().equals(NotLoginException.TOKEN_FREEZE)) {
return Result.failed(Status.TOKEN_FREEZED);
} else if (notLoginException.getType().equals(NotLoginException.NO_PREFIX)) {
return Result.failed(Status.NO_PREFIX);
} else {
return Result.failed(Status.USER_NOT_LOGIN);
}

String type = notLoginException.getType();
Status status = ERR_CODE_MAPPING.getOrDefault(type, Status.NOT_TOKEN);
return Result.failed(status);
}

/**
Expand Down

0 comments on commit 7b38967

Please sign in to comment.