Skip to content

Commit

Permalink
Merge pull request #126 from Team-Tiki/fix/#106-exception-handler
Browse files Browse the repository at this point in the history
[HOTFIX] 예외 처리 설정
  • Loading branch information
paragon0107 authored Jul 19, 2024
2 parents 7d7ccef + 38d055e commit 5e8edf0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tiki.server.common.dto.ErrorResponse;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
Expand Down Expand Up @@ -33,17 +34,25 @@ protected void doFilterInternal(
@NonNull FilterChain filterChain
) throws IOException {
try {
System.out.println("EHF");
filterChain.doFilter(request, response);
} catch (AuthException e) {
log.info("ExceptionHandlerFilter: AuthException - " + e);
handleAuthException(response, e);
} catch (JwtException e) {
log.info("ExceptionHandlerFilter: JWTException - " + e);
handleJwtException(response);
} catch (Exception e) {
} catch (IllegalArgumentException e) {
log.info("ExceptionHandlerFilter: IllegalArgumentException - " + e);
handleIllegalArgumentException(response);
} catch (ServletException e) {
log.info("ExceptionHandlerFilter: Exception - " + e);
handleUncaughtException(response, e);
throw new RuntimeException(e);
}
// catch (Exception e) {
// log.info("ExceptionHandlerFilter: Exception - " + e);
// handleUncaughtException(response);
// }
}

private void handleAuthException(HttpServletResponse response, AuthException e) throws IOException {
Expand All @@ -57,7 +66,12 @@ private void handleJwtException(HttpServletResponse response) throws IOException
setResponse(response, jwtException.getHttpStatus(), jwtException.getMessage());
}

private void handleUncaughtException(HttpServletResponse response, Exception e) throws IOException {
private void handleIllegalArgumentException(HttpServletResponse response) throws IOException {
val uncaughtException = ErrorCode.EMPTY_JWT;
setResponse(response, uncaughtException.getHttpStatus(), uncaughtException.getMessage());
}

private void handleUncaughtException(HttpServletResponse response) throws IOException {
val uncaughtException = ErrorCode.UNCAUGHT_EXCEPTION;
setResponse(response, uncaughtException.getHttpStatus(), uncaughtException.getMessage());
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/tiki/server/auth/jwt/JwtValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public void validateToken(String token) {
} catch (UnsupportedJwtException exception) {
log.info(exception.getMessage());
throw new AuthException(UNSUPPORTED_JWT_TOKEN);
} catch (IllegalArgumentException exception) {
log.info(exception.getMessage());
throw new AuthException(EMPTY_JWT);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/tiki/server/auth/message/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@AllArgsConstructor
public enum ErrorCode {

/* 400 INTERNAL_SERVER_ERROR : 잘못된 요청입니다. */
UNCAUGHT_EXCEPTION(BAD_REQUEST, "예상치 못한 오류입니다."),

/* 401 UNAUTHORIZED : 인증 없음 */
UNAUTHENTICATED_USER(UNAUTHORIZED, "잘못된 토큰 형식입니다."),
INVALID_KEY(UNAUTHORIZED, "유효하지 않은 키입니다."),
Expand All @@ -23,8 +26,7 @@ public enum ErrorCode {
/* 403 FORBIDDEN : 인가 없음 */
UNAUTHORIZED_USER(FORBIDDEN, "권한이 없는 사용자입니다."),

/* 500 INTERNAL_SERVER_ERROR : 서버 내부 오류입니다. */
UNCAUGHT_EXCEPTION(INTERNAL_SERVER_ERROR, "서버 내부 오류입니다.");
UNCAUGHT_SERVER_EXCEPTION(INTERNAL_SERVER_ERROR,"처리되지 않은 에러ㅜ(서버한테 물어보삼)");

private final HttpStatus httpStatus;
private final String message;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/tiki/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.tiki.server.member.exception.MemberException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -23,11 +24,14 @@

import lombok.RequiredArgsConstructor;
import lombok.val;
import org.thymeleaf.util.StringUtils;

import static com.tiki.server.auth.message.ErrorCode.EMPTY_JWT;
import static com.tiki.server.auth.message.ErrorCode.UNMATCHED_TOKEN;
import static com.tiki.server.member.message.ErrorCode.INVALID_MEMBER;
import static com.tiki.server.member.message.ErrorCode.UNMATCHED_PASSWORD;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -51,7 +55,9 @@ public SignInGetResponse login(LoginRequest request, HttpServletResponse respons
}

public ReissueGetResponse reissueToken(HttpServletRequest request) {
System.out.println("1");
val refreshToken = jwtProvider.getTokenFromRequest(request);
checkTokenEmpty(refreshToken);
val memberId = jwtProvider.getUserFromJwt(refreshToken);
val token = tokenFinder.findById(memberId);
checkRefreshToken(refreshToken, token);
Expand All @@ -64,7 +70,15 @@ private Member checkMemberEmpty(LoginRequest request) {
return memberFinder.findByEmail(request.email()).orElseThrow(() -> new MemberException(INVALID_MEMBER));
}

private void checkTokenEmpty(String token){
if(StringUtils.isEmpty(token)){
throw new AuthException(EMPTY_JWT);
}
}

private void checkRefreshToken(String getRefreshToken, Token token) {
log.info("받은 토큰 : " + getRefreshToken);
log.info("저장 토큰 : " + token.refreshToken());
if (!token.refreshToken().equals(getRefreshToken)) {
throw new AuthException(UNMATCHED_TOKEN);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/tiki/server/common/handler/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tiki.server.common.handler;

import com.tiki.server.auth.exception.AuthException;
import com.tiki.server.mail.exception.MailException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -17,6 +18,8 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import static com.tiki.server.auth.message.ErrorCode.UNCAUGHT_SERVER_EXCEPTION;

@Slf4j
@RestControllerAdvice
public class ErrorHandler {
Expand Down Expand Up @@ -69,4 +72,19 @@ public ResponseEntity<BaseResponse> MailException(MailException exception) {
val errorCode = exception.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}

@ExceptionHandler(AuthException.class)
public ResponseEntity<BaseResponse> AuthException(AuthException exception) {
log.error(exception.getMessage());
val errorCode = exception.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<BaseResponse> Exception(Exception exception) {
log.info("here!!");
log.error(exception.getMessage());
val errorCode = UNCAUGHT_SERVER_EXCEPTION;
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}
}

0 comments on commit 5e8edf0

Please sign in to comment.