Skip to content

Commit

Permalink
Log errors in WebClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvind-wedoe committed Oct 30, 2023
1 parent 8fcf064 commit f694ca2
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import no.nav.klage.dokument.clients.kabaljsontopdf.domain.DocumentValidationRes
import no.nav.klage.dokument.clients.kabaljsontopdf.domain.InnholdsfortegnelseRequest
import no.nav.klage.dokument.domain.PDFDocument
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.http.HttpStatusCode
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
Expand All @@ -19,6 +22,7 @@ class KabalJsonToPdfClient(
companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

fun getPDFDocument(json: String): PDFDocument {
Expand All @@ -28,6 +32,9 @@ class KabalJsonToPdfClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(json)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getPDFDocument.name, secureLogger)
}
.toEntity(ByteArray::class.java)
.map {
val filename = it.headers["filename"]?.first()
Expand All @@ -47,6 +54,9 @@ class KabalJsonToPdfClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(innholdsfortegnelseRequest)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getInnholdsfortegnelse.name, secureLogger)
}
.toEntity(ByteArray::class.java)
.map {
val filename = it.headers["filename"]?.first()
Expand All @@ -65,6 +75,9 @@ class KabalJsonToPdfClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(json)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::validateJsonDocument.name, secureLogger)
}
.bodyToMono<DocumentValidationResponse>()
.block() ?: throw RuntimeException("Response null")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import no.nav.klage.dokument.clients.kabalsmarteditorapi.model.response.CommentO
import no.nav.klage.dokument.clients.kabalsmarteditorapi.model.response.DocumentOutput
import no.nav.klage.oppgave.util.TokenUtil
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatusCode
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
Expand All @@ -24,6 +27,7 @@ class KabalSmartEditorApiClient(
companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

fun createDocument(
Expand All @@ -38,6 +42,9 @@ class KabalSmartEditorApiClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(jsonInput)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::createDocument.name, secureLogger)
}
.bodyToMono<DocumentOutput>()
.block() ?: throw RuntimeException("Document could not be created")
}
Expand All @@ -55,6 +62,9 @@ class KabalSmartEditorApiClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(jsonInput)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::updateDocument.name, secureLogger)
}
.bodyToMono<DocumentOutput>()
.block() ?: throw RuntimeException("Document could not be updated")
}
Expand All @@ -69,6 +79,9 @@ class KabalSmartEditorApiClient(
"Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithKabalSmartEditorApiScope()}"
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getDocument.name, secureLogger)
}
.bodyToMono<DocumentOutput>()
.block() ?: throw RuntimeException("Document could not be retrieved")
}
Expand All @@ -83,6 +96,9 @@ class KabalSmartEditorApiClient(
"Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithKabalSmartEditorApiScope()}"
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::deleteDocument.name, secureLogger)
}
.bodyToMono<Unit>()
.block()
}
Expand All @@ -97,6 +113,9 @@ class KabalSmartEditorApiClient(
"Bearer ${tokenUtil.getAppAccessTokenWithKabalSmartEditorApiScope()}"
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::deleteDocumentAsSystemUser.name, secureLogger)
}
.bodyToMono<Unit>()
.block()
}
Expand All @@ -114,6 +133,9 @@ class KabalSmartEditorApiClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(input)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::createComment.name, secureLogger)
}
.bodyToMono<CommentOutput>()
.block() ?: throw RuntimeException("Comment could not be created")
}
Expand All @@ -128,6 +150,9 @@ class KabalSmartEditorApiClient(
"Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithKabalSmartEditorApiScope()}"
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getAllCommentsWithPossibleThreads.name, secureLogger)
}
.bodyToMono<List<CommentOutput>>()
.block() ?: throw RuntimeException("Comments could not be retrieved")
}
Expand All @@ -146,6 +171,9 @@ class KabalSmartEditorApiClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(input)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::replyToComment.name, secureLogger)
}
.bodyToMono<CommentOutput>()
.block() ?: throw RuntimeException("Comment could not be replied to")
}
Expand All @@ -161,6 +189,9 @@ class KabalSmartEditorApiClient(
"Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithKabalSmartEditorApiScope()}"
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getCommentWithPossibleThread.name, secureLogger)
}
.bodyToMono<CommentOutput>()
.block() ?: throw RuntimeException("Comment could not be retrieved")
}
Expand All @@ -182,6 +213,9 @@ class KabalSmartEditorApiClient(
)
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::deleteCommentWithPossibleThread.name, secureLogger)
}
.bodyToMono<Unit>()
.block()
}
Expand All @@ -200,6 +234,9 @@ class KabalSmartEditorApiClient(
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(input)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::modifyComment.name, secureLogger)
}
.bodyToMono<CommentOutput>()
.block() ?: throw RuntimeException("Comment could not be modified")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package no.nav.klage.dokument.clients.klagefileapi
import io.micrometer.tracing.Tracer
import no.nav.klage.oppgave.util.TokenUtil
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatusCode
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.BodyInserters
Expand All @@ -19,6 +22,7 @@ class FileApiClient(
companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

fun getDocument(id: String, systemUser: Boolean = false): ByteArray {
Expand All @@ -34,6 +38,9 @@ class FileApiClient(
.uri { it.path("/document/{id}").build(id) }
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getDocument.name, secureLogger)
}
.bodyToMono<ByteArray>()
.block() ?: throw RuntimeException("Document could not be fetched")
}
Expand All @@ -53,6 +60,9 @@ class FileApiClient(
.uri { it.path("/document/{id}").build(id) }
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::deleteDocument.name, secureLogger)
}
.bodyToMono<Boolean>()
.block()

Expand Down Expand Up @@ -83,6 +93,9 @@ class FileApiClient(
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.body(BodyInserters.fromMultipartData(bodyBuilder.build()))
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::uploadDocument.name, secureLogger)
}
.bodyToMono<DocumentUploadedResponse>()
.block()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package no.nav.klage.oppgave.clients.arbeidoginntekt

import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.http.HttpStatusCode
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.bodyToMono
Expand All @@ -28,6 +30,9 @@ class ArbeidOgInntektClient(
personIdent
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getAInntektUrl.name, secureLogger)
}
.bodyToMono<String>()
.block() ?: throw RuntimeException("No AInntekt url returned")
}
Expand All @@ -42,6 +47,9 @@ class ArbeidOgInntektClient(
personIdent
)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getAARegisterUrl.name, secureLogger)
}
.bodyToMono<String>()
.block() ?: throw RuntimeException("No AAreg url returned")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import no.nav.klage.oppgave.config.CacheWithJCacheConfiguration
import no.nav.klage.oppgave.util.TokenUtil
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.cache.annotation.Cacheable
import org.springframework.http.HttpStatusCode
import org.springframework.retry.annotation.Retryable
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
Expand Down Expand Up @@ -45,6 +47,9 @@ class MicrosoftGraphClient(
}.header("Authorization", "Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithGraphScope()}")

.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getInnloggetSaksbehandler.name, secureLogger)
}
.bodyToMono<AzureUser>()
.block().let { secureLogger.debug("me: {}", it); it }
?: throw RuntimeException("AzureAD data about authenticated user could not be fetched")
Expand All @@ -67,6 +72,9 @@ class MicrosoftGraphClient(
.build()
}.header("Authorization", "Bearer ${tokenUtil.getSaksbehandlerAccessTokenWithGraphScope()}")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getInnloggetSaksbehandlersGroups.name, secureLogger)
}
.bodyToMono<AzureGroupList>()
.block()?.value?.map { secureLogger.debug("AD Gruppe: {}", it); it }
?: throw RuntimeException("AzureAD data about authenticated users groups could not be fetched")
Expand Down Expand Up @@ -112,6 +120,9 @@ class MicrosoftGraphClient(
.build()
}.header("Authorization", "Bearer ${tokenUtil.getAppAccessTokenWithGraphScope()}")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getDisplayNames.name, secureLogger)
}
.bodyToMono()
} catch (e: Exception) {
logger.warn("Could not fetch displayname for idents: $navIdents", e)
Expand All @@ -131,6 +142,9 @@ class MicrosoftGraphClient(
}
.header("Authorization", "Bearer ${tokenUtil.getAppAccessTokenWithGraphScope()}")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getGroupMembersNavIdents.name, secureLogger)
}
.bodyToMono<AzureGroupMemberList>().block()?.value
?: throw RuntimeException("AzureAD data about group members nav idents could not be fetched")
return azureGroupMember.map { secureLogger.debug("Group member {}", it); it }
Expand All @@ -153,6 +167,9 @@ class MicrosoftGraphClient(
.header("Authorization", "Bearer ${tokenUtil.getAppAccessTokenWithGraphScope()}")
.header("ConsistencyLevel", "eventual")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getEnhetensAnsattesNavIdents.name, secureLogger)
}
.bodyToMono<AzureSlimUserList>()
.block()
.let { userList -> userList?.value?.map { it.onPremisesSamAccountName } }
Expand All @@ -168,6 +185,9 @@ class MicrosoftGraphClient(
}
.header("Authorization", "Bearer ${tokenUtil.getAppAccessTokenWithGraphScope()}")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::getGroupsByUserPrincipalName.name, secureLogger)
}
.bodyToMono<AzureGroupList>().block()?.value?.map { secureLogger.debug("AD Gruppe by navident: {}", it); it }
?: throw RuntimeException("AzureAD data about groups by user principal name could not be fetched")
return aadAzureGroups
Expand All @@ -187,6 +207,9 @@ class MicrosoftGraphClient(
.header("Authorization", "Bearer ${tokenUtil.getAppAccessTokenWithGraphScope()}")
.header("ConsistencyLevel", "eventual")
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::findUserByNavIdent.name, secureLogger)
}
.bodyToMono<AzureUserList>().block()?.value?.firstOrNull()
?.let { secureLogger.debug("Saksbehandler: {}", it); it }
?: throw RuntimeException("AzureAD data about user by nav ident could not be fetched")
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/no/nav/klage/oppgave/clients/ereg/EregClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package no.nav.klage.oppgave.clients.ereg


import no.nav.klage.oppgave.exceptions.EREGOrganizationNotFoundException
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.logErrorResponse
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatusCode
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
Expand All @@ -17,6 +21,12 @@ class EregClient(
@Value("\${spring.application.name}")
lateinit var applicationName: String

companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

fun hentOrganisasjon(orgnummer: String): Organisasjon {
return kotlin.runCatching {
eregWebClient.get()
Expand All @@ -29,6 +39,9 @@ class EregClient(
.accept(MediaType.APPLICATION_JSON)
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.onStatus(HttpStatusCode::isError) { response ->
logErrorResponse(response, ::hentOrganisasjon.name, secureLogger)
}
.bodyToMono<Organisasjon>()
.block() ?: throw EREGOrganizationNotFoundException("Search for organization $orgnummer in Ereg returned null.")
}.fold(
Expand Down
Loading

0 comments on commit f694ca2

Please sign in to comment.