-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b5e95e
refactor: 커스텀 예외를 발생시키도록 수정
nayonsoso 99d7010
refactor: 모든 예외를 잡을 수 있도록 수정
nayonsoso cb12488
refactor: ExceptionHandler가 반환하는 예외 응답 형식 변경
nayonsoso 4cc7680
refactor: ResponseEntityExceptionHandler를 상속하지 않도록 변경
nayonsoso aafa347
feat: ResponseEntityExceptionHandler에서 처리하는 예외를 직접 핸들링
nayonsoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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 | ||
@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 입니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/reviewme/review/exception/ReviewNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("리뷰가 존재하지 않습니다."); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
backend/src/main/java/reviewme/review/repository/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
정도로 자세하게 주석을 적으면 좋겠네요 ~!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
억셉트 하겠습니다🐴