Skip to content

Commit

Permalink
[FEAT] RuntimeException Handler 추가 (#131)
Browse files Browse the repository at this point in the history
## 작업 내용
- Runtime Exception 의 경우 `@Secured` 와 일부 annotation 에서 발생하는 인지하는
exception 에 대해서는 직접 핸들링하고, 핸들링에 실패한 Exception 은 모두 500 처리.
- Revert 한 #108 의 경우 Exception 자체를 catch 하여 일부 잡지 말아야하는 exception 까지
처리하였음
- 사용하지 않는 Exception handler 삭제
- Enumcode 관련한 exception 의 경우 **"DB의 문제로 발생하는 에러"**임 따라서 500을 뱉고 로그로
확실하게 남기도록 함.

- 400번대 error 에 대해서는 log 를 별도로 남기지 않도록 함. (추후 협의가 필요한 부분...)

Closes #131
  • Loading branch information
JuneParkCode authored Aug 29, 2024
1 parent 788788d commit abea313
Showing 1 changed file with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,23 +24,22 @@ public ResponseEntity<ErrorRespDto> handleMethodArgumentNotValidException(
return ResponseEntity.badRequest().body(ErrorRespDto.of(message));
}

@ExceptionHandler(InvalidEnumCodeException.class)
public ResponseEntity<ErrorRespDto> 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<ErrorRespDto> handleAuthorizationDeniedException(AuthorizationDeniedException exception) {
return new ResponseEntity<>(ErrorRespDto.of(exception.getMessage()), HttpStatus.FORBIDDEN);
}

@Deprecated
@ExceptionHandler(HttpBaseException.class)
public ResponseEntity<ApiRespDto<Void>> handleHttpException(HttpBaseException exception) {
ApiRespDto<Void> responseDto = ApiRespDto.<Void>builder()
.statusCode(exception.getStatusCode().value())
.message(exception.getMessage())
.build();

return new ResponseEntity<>(
responseDto,
exception.getStatusCode()
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorRespDto> 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);
}
}

0 comments on commit abea313

Please sign in to comment.