Skip to content

Commit

Permalink
feat: error add 462, 463
Browse files Browse the repository at this point in the history
feat: error add 462, 463
  • Loading branch information
GitJIHO authored Nov 14, 2024
2 parents 73e3d70 + 769cafe commit f60e5a1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,20 @@ public ResponseEntity<ProblemDetail> handleAccessTokenExpiredException(AccessTok
return ResponseEntity.status(HttpStatusCode.valueOf(461)).body(problemDetail);
}

@ExceptionHandler(NoPetException.class)
public ResponseEntity<ProblemDetail> handleNoPetException(NoPetException e) {

ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(462), e.getMessage());
problemDetail.setTitle("No Pet Exception");
return ResponseEntity.status(HttpStatusCode.valueOf(462)).body(problemDetail);
}

@ExceptionHandler(NoWrittenSurveyException.class)
public ResponseEntity<ProblemDetail> handleNoWrittenSurveyException(NoWrittenSurveyException e) {

ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(463), e.getMessage());
problemDetail.setTitle("No Written Survey Exception");
return ResponseEntity.status(HttpStatusCode.valueOf(463)).body(problemDetail);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gdg.kkia.common.exception;

public class NoPetException extends RuntimeException {
public NoPetException(String message) { super(message); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gdg.kkia.common.exception;

public class NoWrittenSurveyException extends RuntimeException {
public NoWrittenSurveyException(String message) { super(message); }
}
7 changes: 4 additions & 3 deletions src/main/java/com/gdg/kkia/pet/service/PetService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gdg.kkia.pet.service;

import com.gdg.kkia.common.exception.BadRequestException;
import com.gdg.kkia.common.exception.NoPetException;
import com.gdg.kkia.common.exception.NotFoundException;
import com.gdg.kkia.member.entity.Member;
import com.gdg.kkia.member.repository.MemberRepository;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void updatePetName(Long memberId, PetNameRequest petNameRequest) {
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

Pet pet = petRepository.findByMember(member)
.orElseThrow(() -> new NotFoundException("해당 멤버가 소유한 팻이 없습니다."));
.orElseThrow(() -> new NoPetException("해당 멤버가 소유한 팻이 없습니다."));

pet.changePetName(petNameRequest.name());
}
Expand All @@ -52,7 +53,7 @@ public int earnExperience(Long memberId, Pet.GrowthButton growthButton) {
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

Pet pet = petRepository.findByMember(member)
.orElseThrow(() -> new NotFoundException("해당 멤버가 소유한 팻이 없습니다."));
.orElseThrow(() -> new NoPetException("해당 멤버가 소유한 팻이 없습니다."));

pointLogService.consumePointAndWriteLog(member, growthButton, pet.isMaxGrowth());
return pet.earnExperience(growthButton);
Expand All @@ -64,7 +65,7 @@ public PetInfoResponse getPetInfo(Long memberId) {
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

Pet pet = petRepository.findByMember(member)
.orElseThrow(() -> new NotFoundException("해당 멤버가 소유한 팻이 없습니다."));
.orElseThrow(() -> new NoPetException("해당 멤버가 소유한 팻이 없습니다."));

return new PetInfoResponse(pet.getName(), pet.getLevel(), pet.getExperience());
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/gdg/kkia/survey/service/SurveyService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdg.kkia.survey.service;

import com.gdg.kkia.common.exception.NoWrittenSurveyException;
import com.gdg.kkia.common.exception.NotFoundException;
import com.gdg.kkia.member.entity.Member;
import com.gdg.kkia.member.repository.MemberRepository;
Expand Down Expand Up @@ -36,7 +37,7 @@ public SurveyResponse getMostRecentlyWrittenSurveyWrittenByUser(Long memberId) {
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

Survey survey = surveyRepository.findTopByMemberAndRoleOrderBySurveyedDatetimeDesc(member, Survey.Role.USER)
.orElseThrow(() -> new NotFoundException("작성된 survey가 없습니다."));
.orElseThrow(() -> new NoWrittenSurveyException("작성된 survey가 없습니다."));

return new SurveyResponse(survey.getId(), survey.getSurveyedDatetime(), survey.getAnswer());
}
Expand All @@ -53,7 +54,7 @@ public SurveyResponse getMostRecentlyWrittenSurveyWrittenByModel(Long memberId)
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버를 찾을 수 없습니다."));

Survey survey = surveyRepository.findTopByMemberAndRoleOrderBySurveyedDatetimeDesc(member, Survey.Role.MODEL)
.orElseThrow(() -> new NotFoundException("작성된 survey가 없습니다."));
.orElseThrow(() -> new NoWrittenSurveyException("작성된 survey가 없습니다."));

return new SurveyResponse(survey.getId(), survey.getSurveyedDatetime(), survey.getAnswer());
}
Expand Down

0 comments on commit f60e5a1

Please sign in to comment.