From 25a0bc52c2b39106d3526472f887555ce5b70993 Mon Sep 17 00:00:00 2001 From: Magnus Rinnan Date: Fri, 18 Oct 2024 13:56:28 +0200 Subject: [PATCH] Justeringer logging skattegrunnlag --- .../grunnlag/aop/DefaultExceptionHandler.kt | 23 ++-- .../grunnlag/aop/DefaultExceptionHandler.kt~ | 109 ++++++++++++++++++ .../exception/RestExceptionHandler.kt | 2 +- 3 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt~ diff --git a/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt b/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt index d36fcb51..fddab220 100644 --- a/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt +++ b/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt @@ -13,10 +13,9 @@ import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.ResponseBody import org.springframework.web.client.HttpStatusCodeException +import org.springframework.web.server.ResponseStatusException -fun Boolean?.ifTrue(block: (Boolean) -> T?): T? { - return if (this == true) block(this) else null -} +fun Boolean?.ifTrue(block: (Boolean) -> T?): T? = if (this == true) block(this) else null @Order(Ordered.HIGHEST_PRECEDENCE) @ControllerAdvice @@ -40,6 +39,18 @@ class DefaultExceptionHandler { .body(payloadFeilmelding) } + @ResponseBody + @ExceptionHandler( + ResponseStatusException::class, + ) + fun handleResponseStatusExceptions(e: ResponseStatusException): ResponseEntity<*> { + LOGGER.warn("Feil ved hent av grunnlag: ${e.message}") + val feilmelding = "Hent av grunnlag feilet!" + val headers = HttpHeaders() + headers.add(HttpHeaders.WARNING, feilmelding) + return ResponseEntity.status(e.statusCode).body(ResponseEntity(e.message, headers, e.statusCode)) + } + @ResponseBody @ExceptionHandler(Exception::class) fun handleOtherExceptions(exception: Exception): ResponseEntity<*> { @@ -81,11 +92,7 @@ class DefaultExceptionHandler { return error } - data class Error( - val status: Int, - val message: String, - val fieldErrors: MutableList = mutableListOf(), - ) { + data class Error(val status: Int, val message: String, val fieldErrors: MutableList = mutableListOf()) { fun addFieldError(objectName: String, field: String, message: String) { val error = CustomFieldError(objectName, field, message) fieldErrors.add(error) diff --git a/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt~ b/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt~ new file mode 100644 index 00000000..bbee363c --- /dev/null +++ b/src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt~ @@ -0,0 +1,109 @@ +package no.nav.bidrag.grunnlag.aop + +import com.fasterxml.jackson.databind.JsonMappingException +import no.nav.bidrag.commons.util.secureLogger +import org.slf4j.LoggerFactory +import org.springframework.core.Ordered +import org.springframework.core.annotation.Order +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.MethodArgumentNotValidException +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.ResponseBody +import org.springframework.web.client.HttpStatusCodeException +import org.springframework.web.server.ResponseStatusException + +fun Boolean?.ifTrue(block: (Boolean) -> T?): T? { + return if (this == true) block(this) else null +} + +@Order(Ordered.HIGHEST_PRECEDENCE) +@ControllerAdvice +@Suppress("unused") +class DefaultExceptionHandler { + companion object { + private val LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler::class.java) + } + + @ResponseBody + @ExceptionHandler(HttpStatusCodeException::class) + fun handleHttpClientErrorException(exception: HttpStatusCodeException): ResponseEntity<*> { + val feilmelding = getErrorMessage(exception) + val payloadFeilmelding = + exception.responseBodyAsString.isEmpty().ifTrue { exception.message } + ?: exception.responseBodyAsString + LOGGER.warn(feilmelding, exception) + secureLogger.warn(exception) { "Feilmelding: $feilmelding. Innhold: $payloadFeilmelding" } + return ResponseEntity.status(exception.statusCode) + .header(HttpHeaders.WARNING, feilmelding) + .body(payloadFeilmelding) + } + + @ResponseBody + @ExceptionHandler( + ResponseStatusException::class, + ) + fun handleResponseStatusExceptions(e: ResponseStatusException): ResponseEntity<*> { + LOGGER.warn("Feil ved hent av grunnlag: ${e.message}") + val feilmelding = "Hent av grunnlag feilet!" + val headers = HttpHeaders() + headers.add(HttpHeaders.WARNING, feilmelding) + return ResponseEntity.status(e.statusCode).body(ResponseEntity(e.message, headers, e.statusCode)) + } + + @ResponseBody + @ExceptionHandler(Exception::class) + fun handleOtherExceptions(exception: Exception): ResponseEntity<*> { + LOGGER.error("Det skjedde en ukjent feil: ${exception.message}", exception) + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .header(HttpHeaders.WARNING, exception.message) + .body(exception.message ?: "Ukjent feil") + } + + private fun getErrorMessage(exception: HttpStatusCodeException): String { + val errorMessage = StringBuilder() + + exception.responseHeaders?.get(HttpHeaders.WARNING)?.firstOrNull() + ?.let { errorMessage.append(it) } + if (exception.statusText.isNotEmpty()) { + if (exception.statusCode == HttpStatus.BAD_REQUEST) { + errorMessage.append("Validering feilet - ") + } + errorMessage.append(exception.statusText) + } + return errorMessage.toString() + } + + private fun createMissingKotlinParameterViolation(ex: JsonMappingException): Error { + val error = Error(HttpStatus.BAD_REQUEST.value(), "validation error") + ex.path.filter { it.fieldName != null }.forEach { + error.addFieldError(it.from.toString(), it.fieldName, ex.message.toString()) + } + return error + } + + private fun parseMethodArgumentNotValidException(ex: MethodArgumentNotValidException): Error { + val error = Error(HttpStatus.BAD_REQUEST.value(), "validation error") + ex.fieldErrors.forEach { + val message: String = + if (it.defaultMessage == null) it.toString() else it.defaultMessage!! + error.addFieldError(it.objectName, it.field, message) + } + return error + } + + data class Error( + val status: Int, + val message: String, + val fieldErrors: MutableList = mutableListOf(), + ) { + fun addFieldError(objectName: String, field: String, message: String) { + val error = CustomFieldError(objectName, field, message) + fieldErrors.add(error) + } + } + + data class CustomFieldError(val objectName: String, val field: String, val message: String) +} diff --git a/src/main/kotlin/no/nav/bidrag/grunnlag/exception/RestExceptionHandler.kt b/src/main/kotlin/no/nav/bidrag/grunnlag/exception/RestExceptionHandler.kt index 1d31ef3a..e92d408f 100644 --- a/src/main/kotlin/no/nav/bidrag/grunnlag/exception/RestExceptionHandler.kt +++ b/src/main/kotlin/no/nav/bidrag/grunnlag/exception/RestExceptionHandler.kt @@ -127,7 +127,7 @@ class RestExceptionHandler(private val exceptionLogger: ExceptionLogger) { ResponseStatusException::class, ) fun handleResponseStatusExceptions(e: ResponseStatusException): ResponseEntity<*> { - logger.warn("Feil ved hent av grunllag: ${e.message}") + logger.warn("Feil ved hent av grunnlag: ${e.message}") val feilmelding = "Hent av grunnlag feilet!" val headers = HttpHeaders() headers.add(HttpHeaders.WARNING, feilmelding)