From abea313fe303e9d09127f61ea84ebce62d6680f4 Mon Sep 17 00:00:00 2001 From: Photogrammer <81505228+JuneParkCode@users.noreply.github.com> Date: Thu, 29 Aug 2024 18:49:15 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20RuntimeException=20Handler=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#131)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 작업 내용 - Runtime Exception 의 경우 `@Secured` 와 일부 annotation 에서 발생하는 인지하는 exception 에 대해서는 직접 핸들링하고, 핸들링에 실패한 Exception 은 모두 500 처리. - Revert 한 #108 의 경우 Exception 자체를 catch 하여 일부 잡지 말아야하는 exception 까지 처리하였음 - 사용하지 않는 Exception handler 삭제 - Enumcode 관련한 exception 의 경우 **"DB의 문제로 발생하는 에러"**임 따라서 500을 뱉고 로그로 확실하게 남기도록 함. - 400번대 error 에 대해서는 log 를 별도로 남기지 않도록 함. (추후 협의가 필요한 부분...) Closes #131 --- .../handler/RestControllerAdvice.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/talkka/server/common/exception/handler/RestControllerAdvice.java b/server/src/main/java/com/talkka/server/common/exception/handler/RestControllerAdvice.java index ae18dc50..6036141c 100644 --- a/server/src/main/java/com/talkka/server/common/exception/handler/RestControllerAdvice.java +++ b/server/src/main/java/com/talkka/server/common/exception/handler/RestControllerAdvice.java @@ -2,14 +2,12 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.authorization.AuthorizationDeniedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; -import com.talkka.server.common.dto.ApiRespDto; import com.talkka.server.common.dto.ErrorRespDto; -import com.talkka.server.common.exception.enums.InvalidEnumCodeException; -import com.talkka.server.common.exception.http.HttpBaseException; import lombok.extern.slf4j.Slf4j; @@ -26,23 +24,22 @@ public ResponseEntity handleMethodArgumentNotValidException( return ResponseEntity.badRequest().body(ErrorRespDto.of(message)); } - @ExceptionHandler(InvalidEnumCodeException.class) - public ResponseEntity handleInvalidEnumCodeException(InvalidEnumCodeException exception) { - log.error("InvalidEnumCodeException: {}", exception.getMessage()); - return new ResponseEntity<>(ErrorRespDto.of(INTERNAL_SERVER_ERROR_MESSAGE), HttpStatus.INTERNAL_SERVER_ERROR); + @ExceptionHandler(AuthorizationDeniedException.class) + public ResponseEntity handleAuthorizationDeniedException(AuthorizationDeniedException exception) { + return new ResponseEntity<>(ErrorRespDto.of(exception.getMessage()), HttpStatus.FORBIDDEN); } - @Deprecated - @ExceptionHandler(HttpBaseException.class) - public ResponseEntity> handleHttpException(HttpBaseException exception) { - ApiRespDto responseDto = ApiRespDto.builder() - .statusCode(exception.getStatusCode().value()) - .message(exception.getMessage()) - .build(); - - return new ResponseEntity<>( - responseDto, - exception.getStatusCode() + @ExceptionHandler(RuntimeException.class) + public ResponseEntity handleRuntimeException(RuntimeException exception) { + log.error(""" + Exception Class : {} + Exception Message : {} + Stack Trace : {} + """, + exception.getClass().getName(), + exception.getMessage(), + exception.getStackTrace() ); + return new ResponseEntity<>(ErrorRespDto.of(INTERNAL_SERVER_ERROR_MESSAGE), HttpStatus.INTERNAL_SERVER_ERROR); } }