Skip to content

Commit

Permalink
Justeringer logging skattegrunnlag
Browse files Browse the repository at this point in the history
  • Loading branch information
rinnan17 committed Oct 18, 2024
1 parent 66c1213 commit 25a0bc5
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> Boolean?.ifTrue(block: (Boolean) -> T?): T? {
return if (this == true) block(this) else null
}
fun <T> Boolean?.ifTrue(block: (Boolean) -> T?): T? = if (this == true) block(this) else null

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
Expand All @@ -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<*> {
Expand Down Expand Up @@ -81,11 +92,7 @@ class DefaultExceptionHandler {
return error
}

data class Error(
val status: Int,
val message: String,
val fieldErrors: MutableList<CustomFieldError> = mutableListOf(),
) {
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)
Expand Down
109 changes: 109 additions & 0 deletions src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt~
Original file line number Diff line number Diff line change
@@ -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 <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(
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<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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 25a0bc5

Please sign in to comment.