Skip to content

Commit

Permalink
merge: (#26) ErrorResponse 형식
Browse files Browse the repository at this point in the history
  • Loading branch information
softpeanut authored Sep 7, 2022
2 parents f55ea24 + 7f72461 commit efd6f24
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package team.comit.simtong.global.error

import org.springframework.validation.BindingResult

/**
*
* 상세한 예외를 보여주기 위한 CustomFieldError
*
* @author kimbeomjin
* @date 2022/09/07
* @version 1.0.0
**/
class CustomFieldError(
val field: String,
val value: String,
val reason: String
) {
companion object {
fun of(field: String, value: String, reason: String): List<CustomFieldError> {
val fieldErrors: MutableList<CustomFieldError> = ArrayList()
fieldErrors.add(CustomFieldError(field, value, reason))
return fieldErrors
}

fun of(bindingResult: BindingResult): List<CustomFieldError> {
val fieldErrors = bindingResult.fieldErrors
return fieldErrors.map { error ->
CustomFieldError(
field = error.field,
value = error.rejectedValue.toString(),
reason = error.defaultMessage!!
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package team.comit.simtong.global.error
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.validation.BindingResult
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
import javax.validation.ConstraintViolation
import javax.validation.Path
import javax.validation.ConstraintViolationException

/**
*
Expand All @@ -17,60 +16,57 @@ import javax.validation.Path
class ErrorResponse(
val status: Int,
val message: String,
val reason: String
val fieldErrors: List<CustomFieldError>
) {

companion object {
fun of(exception: ErrorProperty) = of(
exception = exception,
reason = ""
fun of(exception: ErrorProperty) = ErrorResponse(
status = exception.status(),
message = exception.message(),
fieldErrors = emptyList()
)

fun of(bindingResult: BindingResult): ErrorResponse {
val errorMap = bindingResult.fieldErrors.associate { it.field to it.defaultMessage }

return of(
exception = GlobalErrorCode.BAD_REQUEST,
reason = errorMap.toString()
)
}
fun of(bindingResult: BindingResult): ErrorResponse = of(
exception = GlobalErrorCode.BAD_REQUEST,
fieldErrors = CustomFieldError.of(bindingResult)
)

fun of(violations: Set<ConstraintViolation<*>>): ErrorResponse {
val reason = violations.associate { propertyName(it.propertyPath) to it.message }
fun of(exception: ConstraintViolationException): ErrorResponse {
val fieldErrors = exception.constraintViolations.flatMap { violation ->
val path = violation.propertyPath
println(path)
val field = path.last().name
println(field)
val message = violation.message
println(message)
CustomFieldError.of(field, "", message)
}

return of(
exception = GlobalErrorCode.BAD_REQUEST,
reason = reason.toString()
fieldErrors = fieldErrors
)
}

fun of(exception: MethodArgumentTypeMismatchException): ErrorResponse {
val reason = exception.name + ":" + exception.errorCode + ":" + exception.value
val value = exception.value
val fieldErrors = CustomFieldError.of(exception.name, value.toString(), exception.errorCode)

return of(
exception = GlobalErrorCode.BAD_REQUEST,
reason = reason
fieldErrors = fieldErrors
)
}

fun of(exception: DataIntegrityViolationException): ErrorResponse {
val reason = exception.cause.toString()

return of(
exception = GlobalErrorCode.BAD_REQUEST,
reason = reason
)
}
fun of(exception: DataIntegrityViolationException): ErrorResponse = of(
exception = GlobalErrorCode.BAD_REQUEST,
fieldErrors = CustomFieldError.of("", "", exception.message ?: "")
)

private fun of(exception: ErrorProperty, reason: String) = ErrorResponse(
private fun of(exception: ErrorProperty, fieldErrors: List<CustomFieldError>) = ErrorResponse(
status = exception.status(),
message = exception.message(),
reason = reason
fieldErrors = fieldErrors
)

private fun propertyName(path: Path): String {
val pathToString = path.toString()
return pathToString.substring(pathToString.lastIndexOf('.') + 1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WebErrorHandler {
protected fun handleConstraintViolationException(
exception: ConstraintViolationException
): ErrorResponse? {
return ErrorResponse.of(exception.constraintViolations)
return ErrorResponse.of(exception)
}

/**
Expand Down

0 comments on commit efd6f24

Please sign in to comment.