Skip to content

Commit

Permalink
feature: (#301) HttpMessageNotReadableException 핸들링
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Jan 23, 2023
1 parent 8374a53 commit 63aaf2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import javax.validation.ConstraintViolationException
* @author kimbeomjin
* @author Chokyunghyeon
* @date 2022/08/22
* @version 1.2.3
* @version 1.2.5
**/
@RestControllerAdvice
class WebErrorHandler {
Expand Down Expand Up @@ -92,7 +92,7 @@ class WebErrorHandler {
protected fun handleHttpMessageNotReadableException(
exception: HttpMessageNotReadableException
): ErrorResponse? {
return ErrorResponse.of(GlobalExceptions.BadRequest())
return ErrorResponse.of(exception)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package team.comit.simtong.global.error.dto

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.validation.BindingResult
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
Expand All @@ -16,7 +17,7 @@ import javax.validation.ConstraintViolationException
* @author kimbeomjin
* @author Chokyunghyeon
* @date 2022/08/22
* @version 1.2.3
* @version 1.2.5
**/
class ErrorResponse(
val status: Int,
Expand Down Expand Up @@ -44,10 +45,10 @@ class ErrorResponse(

fun of(exception: ConstraintViolationException): ErrorResponse {
val fieldErrors = exception.constraintViolations.flatMap { violation ->
val path = violation.propertyPath
val field = path.last().name
val field = violation.propertyPath.last().name
val value = violation.invalidValue?.toString()
val message = violation.message
CustomFieldError.of(field, "", message)
CustomFieldError.of(field, value, message)
}

return of(
Expand All @@ -57,10 +58,9 @@ class ErrorResponse(
}

fun of(exception: MethodArgumentTypeMismatchException): ErrorResponse {
val value = exception.value
val fieldErrors = CustomFieldError.of(
field = exception.parameter.parameterName ?: "",
value = value.toString(),
value = exception.value?.toString(),
reason = "${exception.requiredType!!.name} 타입으로 변환할 수 없습니다."
)

Expand All @@ -84,10 +84,14 @@ class ErrorResponse(
fieldErrors = CustomFieldError.of("", "", exception.message ?: "")
)

fun of(exception: IllegalArgumentException): ErrorResponse = of(
exception = GlobalExceptions.BadRequest(),
fieldErrors = CustomFieldError.of("", "", exception.message ?: "")
)
fun of(exception: HttpMessageNotReadableException): ErrorResponse {
val message = exception.mostSpecificCause.message?.substringBeforeLast('\n') ?: ""

return of(
exception = GlobalExceptions.BadRequest(),
fieldErrors = CustomFieldError.of("", "", message)
)
}

private fun of(exception: BusinessException, fieldErrors: List<CustomFieldError>) = ErrorResponse(
status = exception.status,
Expand Down

0 comments on commit 63aaf2e

Please sign in to comment.