Skip to content

Commit

Permalink
Merge pull request #75 from NJUPT-SAST/dev-felmose
Browse files Browse the repository at this point in the history
fix error state code and message
  • Loading branch information
feellmoose authored Nov 23, 2023
2 parents b73ce67 + 8d13328 commit 4c4acbb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/main/java/sast/evento/common/enums/ErrorEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
@Getter
@AllArgsConstructor
public enum ErrorEnum {
/* 内部服务器异常 */
INTERNAL_SERVER_ERROR(500,"HTTP Status 500 – Internal Server Error"),
/* 错误 */
COMMON_ERROR(1000, "error"),
/* 鉴权错误 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpServerErrorException;
import sast.evento.common.enums.ErrorEnum;
import sast.evento.response.GlobalResponse;

Expand Down Expand Up @@ -54,4 +55,9 @@ public <T> GlobalResponse<T> handlerSQLIntegrityConstraintViolationException(SQL
return GlobalResponse.failure(error,error.getErrMsg() + ", id out of range or key information repeated");
}

@ExceptionHandler(HttpServerErrorException.class)
public <T> GlobalResponse<T> handlerHttpServerErrorException(HttpServerErrorException e) {
return GlobalResponse.failure(e);
}

}
18 changes: 13 additions & 5 deletions src/main/java/sast/evento/interceptor/HttpInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import sast.evento.annotation.EventId;
import sast.evento.common.enums.ErrorEnum;
import sast.evento.config.ActionRegister;
import sast.evento.entitiy.User;
import sast.evento.exception.LocalRunTimeException;
import sast.evento.model.Action;
import sast.evento.model.UserModel;
import sast.evento.service.LoginService;
import sast.evento.service.PermissionService;
import sast.evento.utils.JsonUtil;
import sast.evento.utils.JwtUtil;
import sast.sastlink.sdk.model.UserInfo;

import java.lang.reflect.Method;
import java.util.Arrays;
Expand Down Expand Up @@ -54,7 +54,7 @@ public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServl
}
Method method = ((HandlerMethod) handler).getMethod();
String token = request.getHeader("TOKEN");
if (method.getName().equals("error")) throw new LocalRunTimeException(ErrorEnum.INTERNAL_SERVER_ERROR);
if (method.getName().equals("error")) return resolveHttpError(response);
Action action = Optional.ofNullable(ActionRegister.actionName2action.get(method.getName()))
.orElseThrow(() -> new LocalRunTimeException(ErrorEnum.METHOD_NOT_EXIST, "unsupported service"));
UserModel user = null;
Expand Down Expand Up @@ -102,13 +102,21 @@ public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServl
}
}
}
if(user == null){
throw new LocalRunTimeException(ErrorEnum.LOGIN_ERROR,"cant check user login info");
if (user == null) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_ERROR, "cant check user login info");
}
userHolder.set(user);
return true;
}

private boolean resolveHttpError(HttpServletResponse response) {
HttpStatus status = HttpStatus.resolve(response.getStatus());
if (status != null) {
throw new HttpServerErrorException(status);
}
return false;
}

@Override
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler, @Nullable Exception ex) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/sast/evento/response/GlobalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.web.client.HttpStatusCodeException;
import sast.evento.common.enums.ErrorEnum;

/**
Expand Down Expand Up @@ -54,7 +55,7 @@ public static <T> GlobalResponse<T> failure(String message) {
return response;
}

public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum,String message) {
public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum, String message) {
GlobalResponse<T> response = new GlobalResponse<>();
response.setSuccess(false);
response.setErrCode(errorEnum.getErrCode());
Expand All @@ -63,7 +64,7 @@ public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum,String message)
return response;
}

public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum,String message,T data) {
public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum, String message, T data) {
GlobalResponse<T> response = new GlobalResponse<>();
response.setSuccess(false);
response.setErrCode(errorEnum.getErrCode());
Expand All @@ -72,4 +73,13 @@ public static <T> GlobalResponse<T> failure(ErrorEnum errorEnum,String message,T
return response;
}

public static <T, E extends HttpStatusCodeException> GlobalResponse<T> failure(E httpStatusCodeException) {
GlobalResponse<T> response = new GlobalResponse<>();
response.setSuccess(false);
response.setErrCode(httpStatusCodeException.getStatusCode().value());
response.setErrMsg(httpStatusCodeException.getMessage());
response.setData(null);
return response;
}

}

0 comments on commit 4c4acbb

Please sign in to comment.