- 패키지
- domain 안에서 exception을 넣고 각각의 domain 별로 핸들러를 만든 것을 확인했습니다.
- 책임을 나누고, 유지보수 관점에서 handler를 쪼갠 것은 이해하였으나, exception을 domain 안에 패키지로 물고 있는 것이 다소 성격이 어울리지 않는다고 생각했는데 의도가 있으신 건지요..?
- dto 안에 request response 클래스가 섞여있는데 dto안에서 패키지를 request와 response 나눠서 사용하는 것은 어떤지
- converter를 사용하는 것은 좋은데 메서드 네이밍을 바꿔보는 것은 어떤지 → 읽어야 될 정보가 너무 많다고 생각이 들었습니다
to{domain}
toResponse 와같이.
- Message(저의 경우 Errorcode) status도 여기서 같이 관리하는 것은 어떤지 - 한 눈에 보기 편해서 좋았다.
public enum Errorcode{ METHOD_NOT_ALLOWED(405, "적절하지 않은 HTTP 메소드입니다."), INVALID_TYPE_VALUE(400, "요청 값의 타입이 잘못되었습니다."), POST_NOT_FOUND(404, "해당 게시글이 존재하지 않습니다."), USER_NOT_FOUND(404, "해당 유저가 존재하지 않습니다."); private int status; private String message; ErrorCode(int status, String message) { this.status = status; this.message = message; } // message Map을 만들어 놓습니다. private static final Map<String, ErrorCode> messageMap = Collections.unmodifiableMap(Stream.of(values()) .collect(Collectors.toMap(ErrorCode::getMessage, Function.identity()))); // message에 해당하는 ErrorCode를 반환합니다. public static ErrorCode fromMessage(String message) { return messageMap.get(message); } }
@Getter public class NotFoundException extends RuntimeException { private final ErrorCode errorCode; public NotFoundException(ErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } }
// 실제 사용 예! private User getCurrentUser(Long userId) { return userRepository.findById(userId) .orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND)); }
@ExceptionHandler(NotFoundException.class) public ResponseEntity<ErrorResponse> handleNotFoundException(NotFoundException e) { log.warn(e.getMessage(), e); ErrorResponse errorResponse = ErrorResponse.of(e.getErrorCode()); return ResponseEntity.status(errorResponse.getStatus()).body(errorResponse); }
- 상태 코드(프론트와 얘기해봐야하지만)
200 ok : (nocontent로 빈 데이터로 내보냄)
201 created : 생성후 location까지
204 no content
400 : 닉네임 중복, 이미 존재할 경우, 해당 요청이 올바르지 않을때(게시글에 좋아요를 눌러야되는데 이미 눌러져있거나, 지울때 좋아요가 눌리지 않는 경우)
404 : 없는 리소스를 요청할때
405 : 적절하지 않은 Http 메서드일 경우
기존에 저는 에러메시지의 경우 보안상 문제로 너무 자세한 메시지를 내려보내주진 않았습니다. 어디까지 자세히 내보내줘야할 지는 프론트 분들과 회의를 해봐야할 것같아요!
//spring implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } // WAS: undertow implementation 'org.springframework.boot:spring-boot-starter-undertow'