Skip to content

Commit

Permalink
feat: handle ConstraintDeclarationException
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB committed Jun 29, 2024
1 parent 0eb7139 commit 1e4752c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.piikii.input.http.advice
import com.piikii.common.exception.PiikiiException
import com.piikii.common.logutil.SystemLogger.logger
import com.piikii.input.http.dto.response.ExceptionResponse
import jakarta.validation.ConstraintDeclarationException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
Expand All @@ -24,6 +25,22 @@ class ExceptionAdvice {
)
}

@ExceptionHandler(ConstraintDeclarationException::class)
fun handleConstraintDeclarationException(
exception: ConstraintDeclarationException,
): ResponseEntity<ExceptionResponse> {
logger.error(exception) { "RequestBody Validation Failure" }
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(
ExceptionResponse(
message = "요청 형식이 잘못됬습니다 (Request Validation Failure)",
cause = exception.message,
timestamp = System.currentTimeMillis(),
),
)
}

@ExceptionHandler(Exception::class)
fun handleException(exception: Exception): ResponseEntity<ExceptionResponse> {
logger.error(exception) { "Occurred Exception" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonInclude

@JsonInclude(JsonInclude.Include.NON_NULL)
class ExceptionResponse(
val message: String,
val message: String?,
val cause: String?,
val timestamp: Long,
)

0 comments on commit 1e4752c

Please sign in to comment.