Skip to content

Commit

Permalink
Bedre logging av feil (#348)
Browse files Browse the repository at this point in the history
* Bedre logging av feil

* Bedre logging av feil

* Bedre logging av feil
  • Loading branch information
ugur93 authored Jun 25, 2024
1 parent 27296b9 commit 3820876
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 29 deletions.
14 changes: 1 addition & 13 deletions src/main/kotlin/no/nav/bidrag/grunnlag/BidragGrunnlagConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class BidragGrunnlagConfig {
@Value("\${FAMILIEBASAK_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): FamilieBaSakConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -104,7 +103,6 @@ class BidragGrunnlagConfig {
@Value("\${FAMILIEEFSAK_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): FamilieEfSakConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -117,7 +115,6 @@ class BidragGrunnlagConfig {
@Value("\${INNTEKTSKOMPONENTEN_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): InntektskomponentenConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -130,7 +127,6 @@ class BidragGrunnlagConfig {
@Value("\${SIGRUN_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): SigrunConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -143,7 +139,6 @@ class BidragGrunnlagConfig {
@Value("\${PENSJON_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): PensjonConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -156,7 +151,6 @@ class BidragGrunnlagConfig {
@Value("\${BIDRAGPERSON_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): BidragPersonConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -169,7 +163,6 @@ class BidragGrunnlagConfig {
@Value("\${FAMILIEKSSAK_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): FamilieKsSakConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -182,7 +175,6 @@ class BidragGrunnlagConfig {
@Value("\${AAREG_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
grunnlagSecurityTokenService: SecurityTokenService,
exceptionLogger: ExceptionLogger,
): ArbeidsforholdConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
Expand All @@ -191,11 +183,7 @@ class BidragGrunnlagConfig {
}

@Bean
fun enhetsregisterConsumer(
@Value("\${EREG_URL}") url: String,
restTemplate: HttpHeaderRestTemplate,
exceptionLogger: ExceptionLogger,
): EnhetsregisterConsumer {
fun enhetsregisterConsumer(@Value("\${EREG_URL}") url: String, restTemplate: HttpHeaderRestTemplate): EnhetsregisterConsumer {
LOGGER.info("Url satt i config: $url")
restTemplate.uriTemplateHandler = RootUriTemplateHandler(url)
return EnhetsregisterConsumer(restTemplate)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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

fun <T> 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(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<CustomFieldError> = 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)
}

This file was deleted.

0 comments on commit 3820876

Please sign in to comment.