11package br.com.webbudget.application.controllers.advice
22
33import br.com.webbudget.domain.exceptions.BusinessException
4+ import io.github.oshai.kotlinlogging.KotlinLogging
45import jakarta.servlet.http.HttpServletResponse
56import org.springframework.dao.NonTransientDataAccessException
67import org.springframework.http.HttpStatus
@@ -10,6 +11,8 @@ import org.springframework.security.authentication.BadCredentialsException
1011import org.springframework.web.bind.annotation.ExceptionHandler
1112import org.springframework.web.bind.annotation.RestControllerAdvice
1213
14+ private val logger = KotlinLogging .logger {}
15+
1316@RestControllerAdvice
1417class ExceptionHandlerAdvice {
1518
@@ -19,16 +22,22 @@ class ExceptionHandlerAdvice {
1922 }
2023
2124 @ExceptionHandler(IllegalArgumentException ::class )
22- fun handle (ex : IllegalArgumentException ): ProblemDetail =
23- ProblemDetail .forStatusAndDetail(BAD_REQUEST , ex.message ? : NO_DETAIL_PROVIDED )
25+ fun handle (ex : IllegalArgumentException ): ProblemDetail {
26+ logger.error(ex) { " Illegal argument exception" }
27+ return ProblemDetail .forStatusAndDetail(BAD_REQUEST , ex.message ? : NO_DETAIL_PROVIDED )
28+ }
2429
2530 @ExceptionHandler(NonTransientDataAccessException ::class )
26- fun handle (ex : NonTransientDataAccessException ): ProblemDetail =
27- ProblemDetail .forStatusAndDetail(BAD_REQUEST , ex.message ? : NO_DETAIL_PROVIDED )
31+ fun handle (ex : NonTransientDataAccessException ): ProblemDetail {
32+ logger.error(ex) { " Non transient data access exception" }
33+ return ProblemDetail .forStatusAndDetail(BAD_REQUEST , ex.message ? : NO_DETAIL_PROVIDED )
34+ }
2835
2936 @ExceptionHandler(BusinessException ::class )
30- fun handle (ex : BusinessException ): ProblemDetail =
31- ProblemDetail .forStatusAndDetail(ex.httpStatus, ex.message ? : NO_DETAIL_PROVIDED )
37+ fun handle (ex : BusinessException ): ProblemDetail {
38+ logger.error(ex) { " Business exception" }
39+ return ProblemDetail .forStatusAndDetail(ex.httpStatus, ex.message ? : NO_DETAIL_PROVIDED )
40+ }
3241
3342 companion object {
3443 private const val NO_DETAIL_PROVIDED = " No detail message provided"
0 commit comments