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

[BE] refactor: 발생하는 모든 예외의 형식 통일 #69

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,11 +2,24 @@

import java.util.List;
import java.util.Map;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.server.MissingRequestValueException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import reviewme.global.exception.BadRequestException;
import reviewme.global.exception.FieldErrorResponse;
import reviewme.global.exception.NotFoundException;

Expand All @@ -18,8 +31,50 @@ public ProblemDetail handleNotFoundException(NotFoundException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getErrorMessage());
}

@ExceptionHandler(BadRequestException.class)
public ProblemDetail handleBadRequestException(BadRequestException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getErrorMessage());
}

@ExceptionHandler(Exception.class)
public ProblemDetail handleException(Exception ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다.");
}

// default error
Copy link
Contributor

Choose a reason for hiding this comment

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

// Exception from Spring framework below 정도로 자세하게 주석을 적으면 좋겠네요 ~!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

억셉트 하겠습니다🐴

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ProblemDetail handleHttpRequestMethodNotSupportedException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 HTTP 메서드입니다.");
}

@ExceptionHandler(HttpMediaTypeException.class)
public ProblemDetail handleHttpMediaTypeException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "잘못된 media type 입니다.");
Copy link
Contributor

Choose a reason for hiding this comment

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

지원하지 않는 MediaType입니다 는 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

브릴리언트 합니다🐴

}

@ExceptionHandler({MissingRequestValueException.class, MissingServletRequestPartException.class})
public ProblemDetail handleMissingRequestException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "필수 요청 데이터가 누락되었습니다.");
}

@ExceptionHandler({ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public ProblemDetail handleServletRequestBindingException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청을 읽을 수 없습니다.");
}

@ExceptionHandler({MethodValidationException.class, BindException.class,
TypeMismatchException.class, HandlerMethodValidationException.class})
public ProblemDetail handleRequestFormatException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청의 형식이 잘못되었습니다.");
}

@ExceptionHandler({NoHandlerFoundException.class, NoResourceFoundException.class})
public ProblemDetail handleNoHandlerFoundException(RuntimeException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "잘못된 경로의 요청입니다.");
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
public ProblemDetail handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<FieldErrorResponse> fieldErrors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package reviewme.review.exception;

import reviewme.global.exception.NotFoundException;

public class ReviewNotFoundException extends NotFoundException {

public ReviewNotFoundException() {
super("리뷰가 존재하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package reviewme.review.repository;

import jakarta.persistence.EntityNotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import reviewme.member.domain.Member;
import reviewme.member.domain.ReviewerGroup;
import reviewme.review.domain.Review;
import reviewme.review.exception.ReviewNotFoundException;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {

boolean existsByReviewerAndReviewerGroup(Member reviewer, ReviewerGroup reviewerGroup);

default Review getReviewById(Long id) {
return findById(id).orElseThrow(EntityNotFoundException::new);
return findById(id).orElseThrow(ReviewNotFoundException::new);
}
}