Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Third seminar 과제 제출 #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import sopt.org.ThirdSeminar.common.dto.ApiResponseDto;
import sopt.org.ThirdSeminar.exception.BusinessException;
import sopt.org.ThirdSeminar.exception.ErrorStatus;

@RestControllerAdvice
Expand All @@ -17,6 +18,11 @@ public class ControllerExceptionAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ApiResponseDto handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION);
return ApiResponseDto.error(ErrorStatus.VALIDATION_EXCEPTION);
}

@ExceptionHandler(BusinessException.class)
protected ApiResponseDto handleBusinessException(final BusinessException e) {
return ApiResponseDto.error(e.getErrorStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.org.ThirdSeminar.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class BusinessException extends RuntimeException {
private final ErrorStatus errorStatus;
jiyeoon00 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public enum ErrorStatus {
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 서버 에러가 발생했습니다."),
BAD_GATEWAY_EXCEPTION(HttpStatus.BAD_GATEWAY, "일시적인 에러가 발생하였습니다.\n잠시 후 다시 시도해주세요!"),
SERVICE_UNAVAILABLE_EXCEPTION(HttpStatus.SERVICE_UNAVAILABLE, "현재 점검 중입니다.\n잠시 후 다시 시도해주세요!"),

/*
NOT_FOUND
*/
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 유저가 존재하지 않습니다."),
POST_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 게시물이 존재하지 않습니다."),
;
jiyeoon00 marked this conversation as resolved.
Show resolved Hide resolved

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sopt.org.ThirdSeminar.controller.post.dto.response.PostResponseDto;
import sopt.org.ThirdSeminar.domain.Post;
import sopt.org.ThirdSeminar.domain.User;
import sopt.org.ThirdSeminar.exception.BusinessException;
import sopt.org.ThirdSeminar.exception.ErrorStatus;
import sopt.org.ThirdSeminar.infrastructure.PostRepository;

@Service
Expand All @@ -33,7 +35,8 @@ public PostResponseDto create(Long userId, PostCreateDto postCreateDto) {

@Transactional(readOnly = true)
public PostResponseDto getOne(Long postId) {
Post post = postRepository.findById(postId).orElseThrow();
Post post = postRepository.findById(postId)
.orElseThrow(() -> new BusinessException(ErrorStatus.POST_NOT_FOUND));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비즈니스 로직 예외를 발생시켜 정리했네용. 혹시try caych문으로 에러를 발생~처리를 하지 않는 이유는 무엇인지 궁금해요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나도 수화 코멘트 보고 생각해봤는데, @RestControllerAdvice가 붙은 클래스에서 어짜피 처리해주기 때문인가? 어떻게 생각하는지 궁금해!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 BusinessException은 관련 ErrorStatus 내용을 담은 Dto를 response해주는 식으로 균일하게 처리할 건데, 매번 똑같은 로직을 try-catch로 잡아서 처리해주는 것보다는 @RestControllerAdvice가 붙은 클래스에서 전역적으로 한 번에 처리 해 줄려고!?


return PostResponseDto.of(post.getTitle(), post.getContent(), post.getUser().getNickname());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import sopt.org.ThirdSeminar.controller.user.dto.request.UserRequestDto;
import sopt.org.ThirdSeminar.controller.user.dto.response.UserResponseDto;
import sopt.org.ThirdSeminar.domain.User;
import sopt.org.ThirdSeminar.exception.BusinessException;
import sopt.org.ThirdSeminar.exception.ErrorStatus;
import sopt.org.ThirdSeminar.infrastructure.UserRepository;

@Service
Expand All @@ -28,6 +30,7 @@ public UserResponseDto create(UserRequestDto request) {
}

public User findUserById(Long userId) {
return userRepository.findById(userId).orElseThrow();
return userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorStatus.USER_NOT_FOUND));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orElseThrow 로 바로 날리는 방법도 있는 거 알아갑니다..!