feat: Submit week 4 assignment - Yeonjae #23
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.
⭐ 과제 내용
✅ PR Point
😡 트러블 슈팅
<문제 발생 코드>
<발생 오류>
중복되는 코드를 처리하고자 예외 처리를 담당하는 get_book 과 get_reveiw를 만들었습니다.
request로 넘겨준 id에 해당하는 책이나 리뷰가 존재하지 않을 경우 AttributeError가 발생했습니다.
get_book 메소드를 호출하고, 책이 존재하지 않을 경우 Response 객체를 반환하기 때문에 오류가 발생했습니다.
클라이언트에게 상황에 맞는 오류 응답이 필요하다고 생각했습니다.
<해결 방법>
Response 객체를 반환하는 대신, Not Found 예외를 발생시켜 메소드의 나머지 부분이 실행되지 않고, 404 응답이 클라이언트에게 반환되도록 변경했습니다.
<문제 해결 코드>
https://www.django-rest-framework.org/api-guide/exceptions/#notfound