Skip to content

Commit 966ac81

Browse files
ref #78
1 parent 6955245 commit 966ac81

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/main/kotlin/br/com/webbudget/application/controllers/advice/ExceptionHandlerAdvice.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package br.com.webbudget.application.controllers.advice
22

33
import br.com.webbudget.domain.exceptions.BusinessException
4+
import io.github.oshai.kotlinlogging.KotlinLogging
45
import jakarta.servlet.http.HttpServletResponse
56
import org.springframework.dao.NonTransientDataAccessException
67
import org.springframework.http.HttpStatus
@@ -10,6 +11,8 @@ import org.springframework.security.authentication.BadCredentialsException
1011
import org.springframework.web.bind.annotation.ExceptionHandler
1112
import org.springframework.web.bind.annotation.RestControllerAdvice
1213

14+
private val logger = KotlinLogging.logger {}
15+
1316
@RestControllerAdvice
1417
class 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

Comments
 (0)