From 7b36b84f04fbb452f163fde7ae16d82b7fb96511 Mon Sep 17 00:00:00 2001 From: JuuuuHong Date: Wed, 4 Oct 2023 10:29:38 +0900 Subject: [PATCH 1/2] add :: valid exception --- .../global/error/handler/GlobalExceptionHandler.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt b/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt index 745e4438..ce4598bc 100644 --- a/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt +++ b/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt @@ -4,6 +4,7 @@ import com.stack.knowledge.global.error.ErrorResponse import com.stack.knowledge.global.error.exception.StackKnowledgeException import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity +import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice @@ -11,8 +12,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice class GlobalExceptionHandler { @ExceptionHandler(StackKnowledgeException::class) fun stackKnowledgeExceptionHandler(e: StackKnowledgeException): ResponseEntity = - ResponseEntity( - ErrorResponse(message = e.errorCode.message, status = e.errorCode.status), - HttpStatus.valueOf(e.errorCode.status) - ) + ResponseEntity.status(HttpStatus.valueOf(e.errorCode.status)).body(ErrorResponse(message = e.errorCode.message, status = e.errorCode.status)) + + @ExceptionHandler(MethodArgumentNotValidException::class) + fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ResponseEntity = + ResponseEntity.status(HttpStatus.BAD_REQUEST.value()).body(ErrorResponse(e.bindingResult.fieldError?.defaultMessage.toString(), HttpStatus.BAD_REQUEST.value())) } \ No newline at end of file From b459bce12985076031193d8df641b5b5d03ec6cb Mon Sep 17 00:00:00 2001 From: JuuuuHong Date: Wed, 4 Oct 2023 19:42:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?add=20::=20valild,=20json=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20exception=20handle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../knowledge/global/error/handler/GlobalExceptionHandler.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt b/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt index ce4598bc..236f9e57 100644 --- a/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt +++ b/src/main/kotlin/com/stack/knowledge/global/error/handler/GlobalExceptionHandler.kt @@ -4,6 +4,7 @@ import com.stack.knowledge.global.error.ErrorResponse import com.stack.knowledge.global.error.exception.StackKnowledgeException import org.springframework.http.HttpStatus 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 @@ -17,4 +18,8 @@ class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException::class) fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ResponseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST.value()).body(ErrorResponse(e.bindingResult.fieldError?.defaultMessage.toString(), HttpStatus.BAD_REQUEST.value())) + + @ExceptionHandler(HttpMessageNotReadableException::class) + fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException) : ResponseEntity = + ResponseEntity.status(HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value())).body(ErrorResponse("json 형식이 잘못되었습니다.", HttpStatus.BAD_REQUEST.value())) } \ No newline at end of file