From eef61197f28d37af9736185e79f57dcf6f7bb150 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Wed, 21 Aug 2024 15:20:46 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20RequestInterceptor=20=EC=97=90=EC=84=9C?= =?UTF-8?q?=20afterCompletion=20=EC=97=90=20=EC=98=88=EC=99=B8=EB=A5=BC=20?= =?UTF-8?q?=EC=9E=A1=EC=95=84=EC=84=9C=20log=20=EB=A5=BC=20=EB=82=A8?= =?UTF-8?q?=EA=B8=B0=EB=8D=98=20=EB=A1=9C=EC=A7=81=EC=9D=84=20GlobalExcept?= =?UTF-8?q?ionHandler=20=EB=A1=9C=20=EC=9D=B4=EB=8F=99,=20=EB=98=90?= =?UTF-8?q?=ED=95=9C=20=ED=95=84=EC=9A=94=EC=97=86=EB=8A=94=20ExceptionHan?= =?UTF-8?q?dler=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LotteryEventController.java | 3 +-- .../eventService/LotteryEventService.java | 5 ++-- .../global/error/GlobalExceptionHandler.java | 25 +++++-------------- .../interceptor/RequestInterceptor.java | 5 ---- .../eventService/LotteryEventServiceTest.java | 3 +-- 5 files changed, 10 insertions(+), 31 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/LotteryEventController.java b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/LotteryEventController.java index fcf7cb3c..7715f21c 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/LotteryEventController.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/LotteryEventController.java @@ -21,7 +21,6 @@ import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; -import java.nio.file.attribute.UserPrincipalNotFoundException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.List; @@ -75,7 +74,7 @@ public ResponseEntity postCasperBot( @ApiResponse(responseCode = "404", description = "User has not applied") }) @GetMapping("/applied") - public ResponseEntity GetLotteryParticipant(HttpServletRequest request) throws UserPrincipalNotFoundException { + public ResponseEntity GetLotteryParticipant(HttpServletRequest request) { BaseUser user = (BaseUser) request.getAttribute("user"); return ResponseEntity .status(HttpStatus.OK) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventService.java index d44a9384..bb3cf4c9 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventService.java @@ -24,7 +24,6 @@ import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; -import java.nio.file.attribute.UserPrincipalNotFoundException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; @@ -64,9 +63,9 @@ public CasperBotResponseDto postCasperBot(BaseUser user, CasperBotRequestDto cas return casperBotDto; } - public LotteryParticipantResponseDto getLotteryParticipant(BaseUser user) throws UserPrincipalNotFoundException { + public LotteryParticipantResponseDto getLotteryParticipant(BaseUser user) { LotteryParticipants participant = lotteryParticipantsRepository.findByBaseUser(user) - .orElseThrow(() -> new UserPrincipalNotFoundException("응모 내역이 없습니다.")); + .orElseThrow(() -> new CustomException("응모 내역이 없습니다.", CustomErrorCode.USER_NOT_FOUND)); return LotteryParticipantResponseDto.of(participant, getCasperBot(participant.getCasperId())); } diff --git a/Server/src/main/java/JGS/CasperEvent/global/error/GlobalExceptionHandler.java b/Server/src/main/java/JGS/CasperEvent/global/error/GlobalExceptionHandler.java index 812deb68..36511a68 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/error/GlobalExceptionHandler.java +++ b/Server/src/main/java/JGS/CasperEvent/global/error/GlobalExceptionHandler.java @@ -2,45 +2,31 @@ import JGS.CasperEvent.global.enums.CustomErrorCode; import JGS.CasperEvent.global.error.exception.CustomException; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; -import org.springframework.web.bind.MissingRequestCookieException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; - -import java.nio.file.attribute.UserPrincipalNotFoundException; - @RestControllerAdvice +@Slf4j public class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) public ResponseEntity handler(CustomException e){ + log.error("CustomException [{}]", e.getMessage(), e); return ResponseEntity .status(HttpStatus.valueOf(e.getErrorCode().getStatus())) .body(ErrorResponse.of(e.getErrorCode(), e.getMessage())); } - @ExceptionHandler(MissingRequestCookieException.class) - public ResponseEntity missingCookieHandler(){ - return ResponseEntity - .status(HttpStatus.UNAUTHORIZED) - .body(ErrorResponse.of(CustomErrorCode.UNAUTHORIZED)); - } - - @ExceptionHandler(UserPrincipalNotFoundException.class) - public ResponseEntity userPrincipalNotFoundHandler(){ - return ResponseEntity - .status(HttpStatus.CONFLICT) - .body(ErrorResponse.of(CustomErrorCode.USER_NOT_FOUND)); - } - @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){ - BindingResult bindingResult = e.getBindingResult(); + log.error("MethodArgumentNotValidException [{}]", e.getMessage(), e); + BindingResult bindingResult = e.getBindingResult(); StringBuilder builder = new StringBuilder(); for (FieldError fieldError : bindingResult.getFieldErrors()) { builder.append(fieldError.getDefaultMessage()); @@ -56,6 +42,7 @@ public ResponseEntity methodArgumentNotValidExceptionHandler(Meth @ExceptionHandler(RuntimeException.class) public ResponseEntity RuntimeExceptionHandler(RuntimeException e){ + log.error("RuntimeException [{}]", e.getMessage(), e); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body(ErrorResponse.of(CustomErrorCode.BAD_REQUEST, e.getMessage())); diff --git a/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java b/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java index c2b80452..ffdbabd4 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java +++ b/Server/src/main/java/JGS/CasperEvent/global/interceptor/RequestInterceptor.java @@ -38,11 +38,6 @@ public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServl @Override public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) { - // 예외 발생 시 로그 추가 - if (ex != null) { - log.error("Exception [{}]", ex.getMessage()); - } - log.info("Response {} [{}]", response.getStatus(), handler); // 요청이 완료된 후 MDC에서 requestId 제거 diff --git a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventServiceTest.java b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventServiceTest.java index 10435cc9..3b223f38 100644 --- a/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventServiceTest.java +++ b/Server/src/test/java/JGS/CasperEvent/domain/event/service/eventService/LotteryEventServiceTest.java @@ -27,7 +27,6 @@ import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; -import java.nio.file.attribute.UserPrincipalNotFoundException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; @@ -128,7 +127,7 @@ void postCasperBot_Success() throws NoSuchPaddingException, IllegalBlockSizeExce @Test @DisplayName("응모 내역 조회 테스트 - 성공") - void getLotteryParticipants_Success() throws UserPrincipalNotFoundException { + void getLotteryParticipants_Success() { //given given(lotteryParticipantsRepository.findByBaseUser(user)) .willReturn(Optional.ofNullable(lotteryParticipants));