From 843bf2ab1060e7d83caff6b6595a082a1e03c0a8 Mon Sep 17 00:00:00 2001 From: KimBeomJin Date: Wed, 7 Sep 2022 21:24:23 +0900 Subject: [PATCH 1/4] add: (#26) CustomFieldError --- .../simtong/global/error/CustomFieldError.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt diff --git a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt new file mode 100644 index 00000000..ca6007ec --- /dev/null +++ b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt @@ -0,0 +1,28 @@ +package team.comit.simtong.global.error + +import org.springframework.validation.BindingResult + +class CustomFieldError( + val field: String, + val value: String, + val reason: String +) { + companion object { + fun of(field: String, value: String, reason: String): List { + val fieldErrors: MutableList = ArrayList() + fieldErrors.add(CustomFieldError(field, value, reason)) + return fieldErrors + } + + fun of(bindingResult: BindingResult): List { + val fieldErrors = bindingResult.fieldErrors + return fieldErrors.map { error -> + CustomFieldError( + field = error.field, + value = error.rejectedValue.toString(), + reason = error.defaultMessage!! + ) + } + } + } +} \ No newline at end of file From 5b33b31a9883ab65cc4b55b6b6cfbb3261f15e65 Mon Sep 17 00:00:00 2001 From: KimBeomJin Date: Wed, 7 Sep 2022 21:24:50 +0900 Subject: [PATCH 2/4] =?UTF-8?q?chore:=20(#26)=20field=20error=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simtong/global/error/ErrorResponse.kt | 64 +++++++++---------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/ErrorResponse.kt b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/ErrorResponse.kt index eebd2750..a9934d57 100644 --- a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/ErrorResponse.kt +++ b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/ErrorResponse.kt @@ -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 /** * @@ -17,60 +16,57 @@ import javax.validation.Path class ErrorResponse( val status: Int, val message: String, - val reason: String + val fieldErrors: List ) { 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>): 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) = 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) - } } } \ No newline at end of file From afc3e467d3fcfbc98080161032577309062ca77e Mon Sep 17 00:00:00 2001 From: KimBeomJin Date: Wed, 7 Sep 2022 21:27:14 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20(#26)=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=9D=B8=EC=9E=90=20exception=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/team/comit/simtong/global/error/WebErrorHandler.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorHandler.kt b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorHandler.kt index 9baf227a..3a09908d 100644 --- a/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorHandler.kt +++ b/simtong-presentation/src/main/kotlin/team/comit/simtong/global/error/WebErrorHandler.kt @@ -66,7 +66,7 @@ class WebErrorHandler { protected fun handleConstraintViolationException( exception: ConstraintViolationException ): ErrorResponse? { - return ErrorResponse.of(exception.constraintViolations) + return ErrorResponse.of(exception) } /** From 7f724610d09ada66d24e31104ce88cefbb8c4231 Mon Sep 17 00:00:00 2001 From: KimBeomJin Date: Wed, 7 Sep 2022 21:28:26 +0900 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20(#26)=20CustomFieldError=20javadoc?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../team/comit/simtong/global/error/CustomFieldError.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt index ca6007ec..b46b1c04 100644 --- a/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt +++ b/simtong-infrastructure/src/main/kotlin/team/comit/simtong/global/error/CustomFieldError.kt @@ -2,6 +2,14 @@ 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,