Skip to content

Commit

Permalink
feat : 예외 타입 처리 기능 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed Jan 18, 2024
1 parent 761d16a commit 3e4a439
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public class AlreadyReIssuedTokenException extends BusinessException {

public AlreadyReIssuedTokenException() {
super(ErrorCode.ALREADY_RE_ISSUED_TOKEN_EXCEPTION);
super(ErrorCode.ALREADY_RE_ISSUED_TOKEN_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void onAuthenticationFailure(
log.info("oauth2 인증 실패", exception);

ErrorResponse errorResponse = ErrorResponse.create(
ErrorCode.OAUTH2_NOT_AUTHENTICATED_EXCEPTION
ErrorCode.OAUTH2_NOT_AUTHENTICATED_ERROR
);

response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ public enum ErrorCode {

//global
INTERNAL_SERVER_ERROR(500, "G001", "서버에 오류가 발생하였습니다."),
INPUT_INVALID_VALUE(400, "G002", "잘못된 입력입니다."),
INPUT_INVALID_VALUE_ERROR(400, "G002", "잘못된 입력 값입니다."),
INPUT_INVALID_TYPE_ERROR(400, "G003", "잘못된 입력 타입입니다."),

//jwt
INVALID_TOKEN_EXCEPTION(400, "J001", "jwt 토큰이 유효하지 않습니다."),
ALREADY_RE_ISSUED_TOKEN_EXCEPTION(400, "J002", "이미 액세스 토큰 재발급에 사용된 리프레시 토큰입니다."),
INVALID_TOKEN_ERROR(400, "J001", "jwt 토큰이 유효하지 않습니다."),
ALREADY_RE_ISSUED_TOKEN_ERROR(400, "J002", "이미 액세스 토큰 재발급에 사용된 리프레시 토큰입니다."),

//auth
AUTHENTICATION_EXCEPTION(401, "A001", "인증에 실패했습니다. 인증 수단이 유효한지 확인하세요."),
AUTHENTICATION_ERROR(401, "A001", "인증에 실패했습니다. 인증 수단이 유효한지 확인하세요."),

//ouath
OAUTH2_NOT_AUTHENTICATED_EXCEPTION(401, "O001", "OAuth2 인증에 실패하였습니다.");
OAUTH2_NOT_AUTHENTICATED_ERROR(401, "O001", "OAuth2 인증에 실패하였습니다.");

private final int status;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package site.timecapsulearchive.core.global.error;

import static site.timecapsulearchive.core.global.error.ErrorCode.INPUT_INVALID_VALUE;
import static site.timecapsulearchive.core.global.error.ErrorCode.INPUT_INVALID_TYPE_ERROR;
import static site.timecapsulearchive.core.global.error.ErrorCode.INPUT_INVALID_VALUE_ERROR;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -35,8 +37,17 @@ protected ResponseEntity<ErrorResponse> handleBusinessException(final BusinessEx
protected ResponseEntity<ErrorResponse> handleRequestArgumentNotValidException(
MethodArgumentNotValidException e) {
log.warn(e.getMessage());
ErrorResponse response = ErrorResponse.create(INPUT_INVALID_VALUE, e.getBindingResult());
return ResponseEntity.status(INPUT_INVALID_VALUE.getStatus())
ErrorResponse response = ErrorResponse.create(INPUT_INVALID_VALUE_ERROR, e.getBindingResult());
return ResponseEntity.status(INPUT_INVALID_VALUE_ERROR.getStatus())
.body(response);
}

@ExceptionHandler
protected ResponseEntity<ErrorResponse> handleRequestTypeNotValidException(
HttpMessageNotReadableException e) {
log.warn(e.getMessage());
ErrorResponse response = ErrorResponse.create(INPUT_INVALID_TYPE_ERROR);
return ResponseEntity.status(INPUT_INVALID_VALUE_ERROR.getStatus())
.body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
public class InvalidTokenException extends BusinessException {

public InvalidTokenException() {
super(ErrorCode.INVALID_TOKEN_EXCEPTION);
super(ErrorCode.INVALID_TOKEN_ERROR);
}

public InvalidTokenException(Throwable throwable) {
super(ErrorCode.INVALID_TOKEN_EXCEPTION, throwable);
super(ErrorCode.INVALID_TOKEN_ERROR, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void unsuccessfulAuthentication(
SecurityContextHolder.clearContext();

ErrorResponse errorResponse = ErrorResponse.create(
ErrorCode.AUTHENTICATION_EXCEPTION
ErrorCode.AUTHENTICATION_ERROR
);

response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Expand Down

0 comments on commit 3e4a439

Please sign in to comment.