Skip to content

Commit

Permalink
feat: CustomException 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
annahxxl committed Feb 21, 2024
1 parent c3d1e85 commit 41f0dbd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kw.api.common.dto.response

class ErrorResponse(
val code: String,
val message: String
)
Original file line number Diff line number Diff line change
@@ -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", "존재하지 않는 질문이 포함되어 있습니다."),
}
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> {
return ResponseEntity.status(ex.getStatus()).body(ex.getErrorResponse())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) }
Expand All @@ -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)
Expand All @@ -115,21 +117,21 @@ 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<Long>): List<Tag> {
val tags = tagRepository.findAllById(tagIds)
if (tags.size != tagIds.size) {
throw IllegalArgumentException("존재하지 않는 태그가 포함되어 있습니다.")
throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_TAG)
}
return tags
}

private fun getExistQuestions(questionIds: List<Long>): List<Question> {
val questions = questionRepository.findAllById(questionIds)
if (questions.size != questionIds.size) {
throw IllegalArgumentException("존재하지 않는 질문이 포함되어 있습니다.")
throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_QUESTION)
}
return questions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -86,13 +88,13 @@ class QuestionService(
private fun getExistTags(tagIds: List<Long>): List<Tag> {
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) }
}
}

0 comments on commit 41f0dbd

Please sign in to comment.