Conversation
Choi-JJunho
left a comment
There was a problem hiding this comment.
전체적으로 이해가 안가는 부분이 많아서 코멘트 남겼습니다.
대표적으로 controller 레벨에서 try - catch 를 사용한것이 이해가 잘 가지 않습니다.
AOP를 활용하는 이유를 잘 이해하지 못하고 있다는 생각이 드는군요
코멘트 참고하여 코드를 리팩터링하고 제안사항 반영해주시면 감사하겠습니다.
| }catch (DataIntegrityViolationException e){ | ||
| final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_EXIST_USER); | ||
| return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); | ||
| } |
There was a problem hiding this comment.
왜 RestControllerAdvice에서 안잡고 여기서 잡고있나요?
There was a problem hiding this comment.
예를 들어, DataIntegrityViolationException 의 예외를 일으키는 다양한 원인이 있는데, 이것을 RestControllerAdvice에서 한번에 받으면, 모든 오류가 원인을 불문하고 한 가지의 ErrorCode로만 처리되게 되는데, 이를 해결할 수 없어서 controller에서 잡았습니다.
There was a problem hiding this comment.
예외가 다소 추상적이라고 생각한다면 custom한 예외를 구현할 수 있을 것 같아요
| NOT_EXIST_ELEMENT(HttpStatus.NOT_FOUND, "존재하지 않는 요소입니다."), | ||
| EMAIL_DUPLICATION(HttpStatus.CONFLICT, "이미 존재하는 이메일입니다."), | ||
|
|
||
| NullPointerException(HttpStatus.BAD_REQUEST, "null인 값이 존재합니다. 다시 입력해주세요."), |
There was a problem hiding this comment.
이 값만 형식이 좀 이상한것 같은데 다른 값들은 UPPER_CASE로 작성하고 이 친구만 PascalCase로 작성한 이유가 있나요?
+) NullPointerException 은 언제 사용되는 에러코드인가요?
사용자가 이 문구를 보고 무엇이 null이길래 에러가 발생한건지 알 수 있을까요?
There was a problem hiding this comment.
NullPointerException 에러를 잡고싶어서 NULL~로 시작하는 값을 만들고 싶었는데, Nu를 치니 NullPointerException을 자동완성할수 있어서 아무생각없이 tab키를 눌러 만든것 같습니다.
There was a problem hiding this comment.
에러를 잡는것은 ControllerAdvice의 인자로 표현하면 됩니다~
| return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); | ||
| } | ||
|
|
||
| //@ExceptionHandler() |
| this.message = errorCode.getMessage(); | ||
| } | ||
|
|
||
| public static ErrorResponse of(final ErrorCode errorCode){ |
There was a problem hiding this comment.
Ctrl + Alt + L로 라인 포맷팅을 해달라는 피드백을 4번째.. 정도 드리고있는 것 같네요 ㅋㅋ
| INSERT INTO article (author_id, board_id, title, content) | ||
| VALUES (?, ?, ?, ?) | ||
| """, | ||
| new String[]{"id"}); | ||
| ps.setLong(1, article.getBoardId()); | ||
| ps.setLong(2, article.getAuthorId()); | ||
| ps.setLong(1, article.getAuthorId()); | ||
| ps.setLong(2, article.getBoardId()); | ||
| ps.setString(3, article.getTitle()); |
There was a problem hiding this comment.
DTO만 바꾸면 되는데 잘 모르고 Domain도 바꿔버린것 같아요.
| @@ -1,10 +1,10 @@ | |||
| package com.example.demo.controller.dto.request; | |||
|
|
|||
| public record ArticleCreateRequest( | |||
There was a problem hiding this comment.
@JsonNaming 어노테이션을 활용하면 굳이 코드레벨에서 스네이크 케이스의 코드가 돌아다닐 필요가 없습니다.
| public record ArticleCreateRequest( | |
| @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) | |
| public record ArticleCreateRequest( |
참고자료: https://www.baeldung.com/jackson-deserialize-snake-to-camel-case#use-jsonnaming-annotation
There was a problem hiding this comment.
@JsonNaming이라는 어노테이션을 처음 보는데, 공부해서 코드에 실제 적용해 볼수 있도록 하겠습니다.
| Long board_id, | ||
| String title, | ||
| String description | ||
| String content |
There was a problem hiding this comment.
변수명을 안바꾸고 구성할 수도 있습니다.
| String content | |
| @JsonProperty("content") | |
| String description |
참고자료: https://www.baeldung.com/jackson-deserialize-snake-to-camel-case#use-jsonproperty-annotation
| public class ErrorExceptionHandler { | ||
|
|
||
| @ExceptionHandler(org.springframework.dao.EmptyResultDataAccessException.class) | ||
| public ResponseEntity<?> EmptyResultDataAccessException(EmptyResultDataAccessException e){ |
There was a problem hiding this comment.
제네릭에 와일드카드를 선언한 이유가 있나요?
| public ResponseEntity<?> EmptyResultDataAccessException(EmptyResultDataAccessException e){ | |
| public ResponseEntity<ErrorResponse> EmptyResultDataAccessException(EmptyResultDataAccessException e){ |
|
pr 반영해서 commit 했습니다! 하지만, 처음 try-catch를 대체할 수 있도록 RestControllerAdvice에서 코드를 구현하는 부분을 어떻게 바꿀수 있는지 고민해봐도 떠오르지 않아서 버디님이라면 어떤 방법으로 구현하실지 궁금합니다. |
Choi-JJunho
left a comment
There was a problem hiding this comment.
잘 적용해주셨네요~
질문주신 내용에 대한 의견 달아봤습니다
| try { | ||
| MemberResponse response = memberService.update(id, request); | ||
| return ResponseEntity.ok(response); | ||
| } catch (DuplicateKeyException e) { |
There was a problem hiding this comment.
pr 반영해서 commit 했습니다! 하지만, 처음 try-catch를 대체할 수 있도록 RestControllerAdvice에서 코드를 구현하는 부분을 어떻게 바꿀수 있는지 고민해봐도 떠오르지 않아서 버디님이라면 어떤 방법으로 구현하실지 궁금합니다.
Service 레이어에서 해당 예외를 catch한 뒤 내가 핸들링하고싶은 상황에 대한 새로운 예외를 만들어서 그 예외를 반환해볼 수 있을 것 같아요
try-catch는 Controller의 책임인가? 도 한번 생각해보시면 좋을 것 같아요 :)
There was a problem hiding this comment.
보통 controller는 최대한 간단하고 역할이 없게 만들고, Service 에서 try-catch를 사용한다고 들었습니다.
No description provided.