generated from navikt/bidrag-stonad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bedre logging av feil * Bedre logging av feil * Bedre logging av feil
- Loading branch information
Showing
3 changed files
with
97 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/kotlin/no/nav/bidrag/grunnlag/aop/DefaultExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
16 changes: 0 additions & 16 deletions
16
src/main/kotlin/no/nav/bidrag/grunnlag/aop/ExceptionLoggerAspect.kt
This file was deleted.
Oops, something went wrong.