Skip to content

Commit

Permalink
refactor: exception custom -> api
Browse files Browse the repository at this point in the history
  • Loading branch information
annahxxl committed Feb 21, 2024
1 parent 4ea987b commit 298d734
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.kw.api.common.exception

import org.springframework.http.HttpStatus

enum class CustomErrorCode(
enum class ApiErrorCode(
val status: HttpStatus,
val code: String,
val message: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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() {
class ApiException(private val errorCode: ApiErrorCode) : RuntimeException() {

fun getErrorResponse(): ErrorResponse {
return ErrorResponse(this.errorCode.code, this.errorCode.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

@RestControllerAdvice
class CustomExceptionHandler : ResponseEntityExceptionHandler() {
class ApiExceptionHandler : ResponseEntityExceptionHandler() {

override fun handleHttpMessageNotReadable(
ex: HttpMessageNotReadableException,
headers: HttpHeaders,
status: HttpStatusCode,
request: WebRequest
): ResponseEntity<Any>? {
val errorCode = CustomErrorCode.BAD_REQUEST
val errorCode = ApiErrorCode.BAD_REQUEST
val errorResponse = ErrorResponse(errorCode.code, errorCode.message)
return ResponseEntity.status(errorCode.status).body(errorResponse)
}
Expand All @@ -31,7 +31,7 @@ class CustomExceptionHandler : ResponseEntityExceptionHandler() {
status: HttpStatusCode,
request: WebRequest
): ResponseEntity<Any>? {
val errorCode = CustomErrorCode.BAD_REQUEST
val errorCode = ApiErrorCode.BAD_REQUEST
val errorResponse = ErrorResponse(errorCode.code, errorCode.message)

ex.bindingResult.fieldErrors.forEach {
Expand All @@ -41,8 +41,8 @@ class CustomExceptionHandler : ResponseEntityExceptionHandler() {
return ResponseEntity.status(errorCode.status).body(errorResponse)
}

@ExceptionHandler(CustomException::class)
protected fun handleCustomException(ex: CustomException): ResponseEntity<ErrorResponse> {
@ExceptionHandler(ApiException::class)
protected fun handleApiException(ex: ApiException): ResponseEntity<ErrorResponse> {
return ResponseEntity.status(ex.getStatus()).body(ex.getErrorResponse())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.common.exception.ApiErrorCode
import com.kw.api.common.exception.ApiException
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 @@ -65,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 CustomException(CustomErrorCode.NOT_FOUND_BUNDLE)
?: throw ApiException(ApiErrorCode.NOT_FOUND_BUNDLE)
return BundleDetailResponse.from(bundle)
}

fun updateBundle(id: Long, request: BundleUpdateRequest): BundleResponse {
val bundle = bundleRepository.findWithTagsById(id)
?: throw CustomException(CustomErrorCode.NOT_FOUND_BUNDLE)
?: throw ApiException(ApiErrorCode.NOT_FOUND_BUNDLE)

request.name?.let { bundle.updateName(request.name) }
request.shareType?.let { bundle.updateShareType(Bundle.ShareType.from(request.shareType)) }
Expand All @@ -90,9 +90,9 @@ class BundleService(

fun scrapeBundle(id: Long) {
val bundle = bundleRepository.findWithTagsById(id)
?: throw CustomException(CustomErrorCode.NOT_FOUND_BUNDLE)
?: throw ApiException(ApiErrorCode.NOT_FOUND_BUNDLE)
if (bundle.shareType == Bundle.ShareType.PRIVATE) {
throw CustomException(CustomErrorCode.FORBIDDEN_BUNDLE)
throw ApiException(ApiErrorCode.FORBIDDEN_BUNDLE)
}

val questions = questionRepository.findAllWithTagsByBundleId(id)
Expand All @@ -117,21 +117,21 @@ class BundleService(

private fun getExistBundle(id: Long): Bundle {
return bundleRepository.findById(id)
.orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) }
.orElseThrow { ApiException(ApiErrorCode.NOT_FOUND_BUNDLE) }
}

private fun getExistTags(tagIds: List<Long>): List<Tag> {
val tags = tagRepository.findAllById(tagIds)
if (tags.size != tagIds.size) {
throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_TAG)
throw ApiException(ApiErrorCode.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 CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_QUESTION)
throw ApiException(ApiErrorCode.INCLUDE_NOT_FOUND_QUESTION)
}
return questions
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +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.common.exception.ApiErrorCode
import com.kw.api.common.exception.ApiException
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 @@ -33,7 +33,7 @@ class QuestionService(
) {
fun createQuestion(request: QuestionCreateRequest): QuestionResponse {
val bundle = bundleRepository.findById(request.bundleId)
.orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_BUNDLE) }
.orElseThrow { ApiException(ApiErrorCode.NOT_FOUND_BUNDLE) }

val question = request.toEntity(bundle)
val tags = request.tagIds?.let { getExistTags(it) } ?: emptyList()
Expand Down Expand Up @@ -88,13 +88,13 @@ class QuestionService(
private fun getExistTags(tagIds: List<Long>): List<Tag> {
val tags = tagRepository.findAllById(tagIds)
if (tags.size != tagIds.size) {
throw CustomException(CustomErrorCode.INCLUDE_NOT_FOUND_TAG)
throw ApiException(ApiErrorCode.INCLUDE_NOT_FOUND_TAG)
}
return tags
}

private fun getExistQuestion(id: Long): Question {
return questionRepository.findById(id)
.orElseThrow { CustomException(CustomErrorCode.NOT_FOUND_QUESTION) }
.orElseThrow { ApiException(ApiErrorCode.NOT_FOUND_QUESTION) }
}
}

0 comments on commit 298d734

Please sign in to comment.