From 769cafeaff74d2429946c958209b81067a3af6c7 Mon Sep 17 00:00:00 2001 From: gitjiho Date: Fri, 15 Nov 2024 02:04:39 +0900 Subject: [PATCH] feat: error add 462, 463 --- .../common/exception/GlobalExceptionHandler.java | 16 ++++++++++++++++ .../kkia/common/exception/NoPetException.java | 5 +++++ .../exception/NoWrittenSurveyException.java | 5 +++++ .../com/gdg/kkia/pet/service/PetService.java | 7 ++++--- .../gdg/kkia/survey/service/SurveyService.java | 5 +++-- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/gdg/kkia/common/exception/NoPetException.java create mode 100644 src/main/java/com/gdg/kkia/common/exception/NoWrittenSurveyException.java diff --git a/src/main/java/com/gdg/kkia/common/exception/GlobalExceptionHandler.java b/src/main/java/com/gdg/kkia/common/exception/GlobalExceptionHandler.java index 20a16b2..07f759e 100644 --- a/src/main/java/com/gdg/kkia/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/gdg/kkia/common/exception/GlobalExceptionHandler.java @@ -66,4 +66,20 @@ public ResponseEntity handleAccessTokenExpiredException(AccessTok return ResponseEntity.status(HttpStatusCode.valueOf(461)).body(problemDetail); } + @ExceptionHandler(NoPetException.class) + public ResponseEntity 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 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); + } + } diff --git a/src/main/java/com/gdg/kkia/common/exception/NoPetException.java b/src/main/java/com/gdg/kkia/common/exception/NoPetException.java new file mode 100644 index 0000000..fea21d2 --- /dev/null +++ b/src/main/java/com/gdg/kkia/common/exception/NoPetException.java @@ -0,0 +1,5 @@ +package com.gdg.kkia.common.exception; + +public class NoPetException extends RuntimeException { + public NoPetException(String message) { super(message); } +} diff --git a/src/main/java/com/gdg/kkia/common/exception/NoWrittenSurveyException.java b/src/main/java/com/gdg/kkia/common/exception/NoWrittenSurveyException.java new file mode 100644 index 0000000..440193a --- /dev/null +++ b/src/main/java/com/gdg/kkia/common/exception/NoWrittenSurveyException.java @@ -0,0 +1,5 @@ +package com.gdg.kkia.common.exception; + +public class NoWrittenSurveyException extends RuntimeException { + public NoWrittenSurveyException(String message) { super(message); } +} diff --git a/src/main/java/com/gdg/kkia/pet/service/PetService.java b/src/main/java/com/gdg/kkia/pet/service/PetService.java index c10ad9e..22e5614 100644 --- a/src/main/java/com/gdg/kkia/pet/service/PetService.java +++ b/src/main/java/com/gdg/kkia/pet/service/PetService.java @@ -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; @@ -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()); } @@ -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); @@ -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()); } diff --git a/src/main/java/com/gdg/kkia/survey/service/SurveyService.java b/src/main/java/com/gdg/kkia/survey/service/SurveyService.java index d3438e6..a5c7b41 100644 --- a/src/main/java/com/gdg/kkia/survey/service/SurveyService.java +++ b/src/main/java/com/gdg/kkia/survey/service/SurveyService.java @@ -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; @@ -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()); } @@ -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()); }