Skip to content

Commit

Permalink
feat: validation exceptionHandler 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
annahxxl committed Feb 21, 2024
1 parent 41f0dbd commit 4ea987b
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ package com.kw.api.common.dto.response

class ErrorResponse(
val code: String,
val message: String
)
val message: String,
val validations: MutableMap<String, String> = mutableMapOf()
) {
fun addValidation(key: String, value: String) {
validations[key] = value
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ enum class CustomErrorCode(
val message: String
) {

// bundle
FORBIDDEN_BUNDLE(HttpStatus.FORBIDDEN, "FORBIDDEN_BUNDLE", "비공개 꾸러미입니다."),
NOT_FOUND_BUNDLE(HttpStatus.NOT_FOUND, "NOT_FOUND_BUNDLE", "존재하지 않는 꾸러미입니다."),

// question
NOT_FOUND_QUESTION(HttpStatus.NOT_FOUND, "NOT_FOUND_QUESTION", "존재하지 않는 질문입니다."),
INCLUDE_NOT_FOUND_QUESTION(HttpStatus.NOT_FOUND, "INCLUDE_NOT_FOUND_QUESTION", "존재하지 않는 질문이 포함되어 있습니다."),

// tag
INCLUDE_NOT_FOUND_TAG(HttpStatus.NOT_FOUND, "INCLUDE_NOT_FOUND_TAG", "존재하지 않는 태그가 포함되어 있습니다."),
INCLUDE_NOT_FOUND_QUESTION(HttpStatus.NOT_FOUND, "INCLUDE_NOT_FOUND_QUESTION", "존재하지 않는 질문이 포함되어 있습니다."),

// common
BAD_REQUEST(HttpStatus.BAD_REQUEST, "BAD_REQUEST", "잘못된 요청입니다."),

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
package com.kw.api.common.exception

import com.kw.api.common.dto.response.ErrorResponse
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatusCode
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

@RestControllerAdvice
class CustomExceptionHandler : ResponseEntityExceptionHandler() {

override fun handleHttpMessageNotReadable(
ex: HttpMessageNotReadableException,
headers: HttpHeaders,
status: HttpStatusCode,
request: WebRequest
): ResponseEntity<Any>? {
val errorCode = CustomErrorCode.BAD_REQUEST
val errorResponse = ErrorResponse(errorCode.code, errorCode.message)
return ResponseEntity.status(errorCode.status).body(errorResponse)
}

override fun handleMethodArgumentNotValid(
ex: MethodArgumentNotValidException,
headers: HttpHeaders,
status: HttpStatusCode,
request: WebRequest
): ResponseEntity<Any>? {
val errorCode = CustomErrorCode.BAD_REQUEST
val errorResponse = ErrorResponse(errorCode.code, errorCode.message)

ex.bindingResult.fieldErrors.forEach {
errorResponse.addValidation(it.field, it.defaultMessage!!)
}

return ResponseEntity.status(errorCode.status).body(errorResponse)
}

@ExceptionHandler(CustomException::class)
protected fun handleCustomException(ex: CustomException): ResponseEntity<ErrorResponse> {
return ResponseEntity.status(ex.getStatus()).body(ex.getErrorResponse())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.kw.api.domain.bundle.dto.request

import com.kw.data.domain.bundle.Bundle
import jakarta.validation.constraints.Max
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size

data class BundleCreateRequest(
@NotBlank(message = "이름은 필수입니다.")
@field:NotBlank(message = "이름은 필수입니다.")
val name: String,

@NotBlank(message = "공개 범위 설정은 필수입니다.")
@field:NotBlank(message = "공개 범위 설정은 필수입니다.")
val shareType: String,

@Max(value = 3, message = "태그는 최대 3개까지 지정 가능합니다.")
@field:Size(max = 3, message = "태그는 최대 3개까지 지정 가능합니다.")
val tagIds: List<Long>?
) {
fun toEntity(): Bundle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package com.kw.api.domain.bundle.dto.request

data class BundleQuestionAddRequest(val questionIds: List<Long>)
import jakarta.validation.constraints.Size

data class BundleQuestionAddRequest(
@field:Size(min = 1, message = "추가할 질문을 선택해 주세요.")
val questionIds: List<Long>
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package com.kw.api.domain.bundle.dto.request

data class BundleQuestionRemoveRequest(val questionIds: List<Long>)
import jakarta.validation.constraints.Size

data class BundleQuestionRemoveRequest(
@field:Size(min = 1, message = "삭제할 질문을 선택해 주세요.")
val questionIds: List<Long>
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package com.kw.api.domain.bundle.dto.request

import jakarta.validation.constraints.Max
import jakarta.validation.constraints.NotBlank

data class BundleUpdateRequest(
@NotBlank(message = "이름은 필수입니다.")
val name: String?,

@NotBlank(message = "공개 범위 설정은 필수입니다.")
val shareType: String?,

@Max(value = 3, message = "태그는 최대 3개까지 지정 가능합니다.")
val tagIds: List<Long>?
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class QuestionController(val questionService: QuestionService) {

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/questions")
fun createQuestion(@RequestBody request: QuestionCreateRequest): ApiResponse<QuestionResponse> {
fun createQuestion(@RequestBody @Valid request: QuestionCreateRequest): ApiResponse<QuestionResponse> {
val response = questionService.createQuestion(request)
return ApiResponse.created(response);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package com.kw.api.domain.question.dto.request
import com.kw.data.domain.bundle.Bundle
import com.kw.data.domain.question.Question
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull

data class QuestionCreateRequest(
@NotBlank(message = "내용은 필수입니다.")
@field:NotBlank(message = "내용은 필수입니다.")
val content: String,

val answer: String?,

@NotBlank(message = "답변 공개 범위 설정은 필수입니다.")
@field:NotBlank(message = "답변 공개 범위 설정은 필수입니다.")
val answerShareStatus: String,

val tagIds: List<Long>?,

@NotNull(message = "꾸러미 선택은 필수입니다.")
val bundleId: Long
) {
fun toEntity(bundle: Bundle): Question {
Expand Down

0 comments on commit 4ea987b

Please sign in to comment.