Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: (#26) ErrorResponse 형식 #40

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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