From 41f0dbda0f2a36ecef4ee448d88cc71c311cc2a0 Mon Sep 17 00:00:00 2001 From: Hanna Lee <8annahxxl@gmail.com> Date: Thu, 22 Feb 2024 00:46:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CustomException=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kw/api/common/dto/response/ErrorResponse.kt | 6 ++++++ .../kw/api/common/exception/CustomErrorCode.kt | 17 +++++++++++++++++ .../kw/api/common/exception/CustomException.kt | 16 ++++++++++++++++ .../common/exception/CustomExceptionHandler.kt | 17 +++++++++++++++++ .../api/domain/bundle/service/BundleService.kt | 16 +++++++++------- .../domain/question/service/QuestionService.kt | 8 +++++--- 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 server/api/src/main/kotlin/com/kw/api/common/dto/response/ErrorResponse.kt create mode 100644 server/api/src/main/kotlin/com/kw/api/common/exception/CustomErrorCode.kt create mode 100644 server/api/src/main/kotlin/com/kw/api/common/exception/CustomException.kt create mode 100644 server/api/src/main/kotlin/com/kw/api/common/exception/CustomExceptionHandler.kt diff --git a/server/api/src/main/kotlin/com/kw/api/common/dto/response/ErrorResponse.kt b/server/api/src/main/kotlin/com/kw/api/common/dto/response/ErrorResponse.kt new file mode 100644 index 0000000..c3b712c --- /dev/null +++ b/server/api/src/main/kotlin/com/kw/api/common/dto/response/ErrorResponse.kt @@ -0,0 +1,6 @@ +package com.kw.api.common.dto.response + +class ErrorResponse( + val code: String, + val message: String +) diff --git a/server/api/src/main/kotlin/com/kw/api/common/exception/CustomErrorCode.kt b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomErrorCode.kt new file mode 100644 index 0000000..52a0a54 --- /dev/null +++ b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomErrorCode.kt @@ -0,0 +1,17 @@ +package com.kw.api.common.exception + +import org.springframework.http.HttpStatus + +enum class CustomErrorCode( + val status: HttpStatus, + val code: String, + val message: String +) { + + FORBIDDEN_BUNDLE(HttpStatus.FORBIDDEN, "FORBIDDEN_BUNDLE", "비공개 꾸러미입니다."), + NOT_FOUND_BUNDLE(HttpStatus.NOT_FOUND, "NOT_FOUND_BUNDLE", "존재하지 않는 꾸러미입니다."), + NOT_FOUND_QUESTION(HttpStatus.NOT_FOUND, "NOT_FOUND_QUESTION", "존재하지 않는 질문입니다."), + + INCLUDE_NOT_FOUND_TAG(HttpStatus.NOT_FOUND, "INCLUDE_NOT_FOUND_TAG", "존재하지 않는 태그가 포함되어 있습니다."), + INCLUDE_NOT_FOUND_QUESTION(HttpStatus.NOT_FOUND, "INCLUDE_NOT_FOUND_QUESTION", "존재하지 않는 질문이 포함되어 있습니다."), +} diff --git a/server/api/src/main/kotlin/com/kw/api/common/exception/CustomException.kt b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomException.kt new file mode 100644 index 0000000..4f9ccdc --- /dev/null +++ b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomException.kt @@ -0,0 +1,16 @@ +package com.kw.api.common.exception + +import com.kw.api.common.dto.response.ErrorResponse +import org.springframework.http.HttpStatus + +class CustomException(private val errorCode: CustomErrorCode) : RuntimeException() { + + fun getErrorResponse(): ErrorResponse { + return ErrorResponse(this.errorCode.code, this.errorCode.message) + } + + fun getStatus(): HttpStatus { + return this.errorCode.status + } + +} diff --git a/server/api/src/main/kotlin/com/kw/api/common/exception/CustomExceptionHandler.kt b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomExceptionHandler.kt new file mode 100644 index 0000000..af71d1c --- /dev/null +++ b/server/api/src/main/kotlin/com/kw/api/common/exception/CustomExceptionHandler.kt @@ -0,0 +1,17 @@ +package com.kw.api.common.exception + +import com.kw.api.common.dto.response.ErrorResponse +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler + +@RestControllerAdvice +class CustomExceptionHandler : ResponseEntityExceptionHandler() { + + @ExceptionHandler(CustomException::class) + protected fun handleCustomException(ex: CustomException): ResponseEntity { + return ResponseEntity.status(ex.getStatus()).body(ex.getErrorResponse()) + } + +} diff --git a/server/api/src/main/kotlin/com/kw/api/domain/bundle/service/BundleService.kt b/server/api/src/main/kotlin/com/kw/api/domain/bundle/service/BundleService.kt index 89a9d6e..f9fd557 100644 --- a/server/api/src/main/kotlin/com/kw/api/domain/bundle/service/BundleService.kt +++ b/server/api/src/main/kotlin/com/kw/api/domain/bundle/service/BundleService.kt @@ -2,6 +2,8 @@ package com.kw.api.domain.bundle.service import com.kw.api.common.dto.request.PageCondition import com.kw.api.common.dto.response.PageResponse +import com.kw.api.common.exception.CustomErrorCode +import com.kw.api.common.exception.CustomException import com.kw.api.domain.bundle.dto.request.BundleCreateRequest import com.kw.api.domain.bundle.dto.request.BundleQuestionAddRequest import com.kw.api.domain.bundle.dto.request.BundleQuestionRemoveRequest @@ -63,13 +65,13 @@ class BundleService( fun getBundle(id: Long, showOnlyMyQuestion: Boolean? = null, memberId: Long? = null): BundleDetailResponse { val bundle = bundleRepository.findDetailById(id, showOnlyMyQuestion, memberId) //TODO: 임시 memberId, 인증 기능 추가 후 수정 - ?: throw IllegalArgumentException("존재하지 않는 꾸러미입니다.") + ?: throw CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) return BundleDetailResponse.from(bundle) } fun updateBundle(id: Long, request: BundleUpdateRequest): BundleResponse { val bundle = bundleRepository.findWithTagsById(id) - ?: throw IllegalArgumentException("존재하지 않는 꾸러미입니다.") + ?: throw CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) request.name?.let { bundle.updateName(request.name) } request.shareType?.let { bundle.updateShareType(Bundle.ShareType.from(request.shareType)) } @@ -88,9 +90,9 @@ class BundleService( fun scrapeBundle(id: Long) { val bundle = bundleRepository.findWithTagsById(id) - ?: throw IllegalArgumentException("존재하지 않는 꾸러미입니다.") + ?: throw CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) if (bundle.shareType == Bundle.ShareType.PRIVATE) { - throw IllegalArgumentException("비공개 꾸러미입니다.") + throw CustomException(CustomErrorCode.FORBIDDEN_BUNDLE) } val questions = questionRepository.findAllWithTagsByBundleId(id) @@ -115,13 +117,13 @@ class BundleService( private fun getExistBundle(id: Long): Bundle { return bundleRepository.findById(id) - .orElseThrow { IllegalArgumentException("존재하지 않는 꾸러미입니다.") } + .orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) } } private fun getExistTags(tagIds: List): List { val tags = tagRepository.findAllById(tagIds) if (tags.size != tagIds.size) { - throw IllegalArgumentException("존재하지 않는 태그가 포함되어 있습니다.") + throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_TAG) } return tags } @@ -129,7 +131,7 @@ class BundleService( private fun getExistQuestions(questionIds: List): List { val questions = questionRepository.findAllById(questionIds) if (questions.size != questionIds.size) { - throw IllegalArgumentException("존재하지 않는 질문이 포함되어 있습니다.") + throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_QUESTION) } return questions } diff --git a/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt b/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt index 07425ed..3babb99 100644 --- a/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt +++ b/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt @@ -1,6 +1,8 @@ package com.kw.api.domain.question.service import com.kw.api.common.dto.PageResponse +import com.kw.api.common.exception.CustomErrorCode +import com.kw.api.common.exception.CustomException import com.kw.api.domain.question.dto.request.QuestionCreateRequest import com.kw.api.domain.question.dto.request.QuestionSearchRequest import com.kw.api.domain.question.dto.request.QuestionUpdateRequest @@ -31,7 +33,7 @@ class QuestionService( ) { fun createQuestion(request: QuestionCreateRequest): QuestionResponse { val bundle = bundleRepository.findById(request.bundleId) - .orElseThrow { IllegalArgumentException("존재하지 않는 꾸러미 입니다.") } + .orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) } val question = request.toEntity(bundle) val tags = request.tagIds?.let { getExistTags(it) } ?: emptyList() @@ -86,13 +88,13 @@ class QuestionService( private fun getExistTags(tagIds: List): List { val tags = tagRepository.findAllById(tagIds) if (tags.size != tagIds.size) { - throw IllegalArgumentException("존재하지 않는 태그가 포함되어 있습니다.") + throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_TAG) } return tags } private fun getExistQuestion(id: Long): Question { return questionRepository.findById(id) - .orElseThrow { IllegalArgumentException("존재하지 않는 질문입니다.") } + .orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_QUESTION) } } }