From d4fa96c2a3859690b7652d336bdf6417c93e2fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Oliver=20S=C3=B8berg?= Date: Thu, 19 Dec 2024 14:30:42 +0100 Subject: [PATCH] =?UTF-8?q?H=C3=A5ndter=20response=20200=20bedre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/trygdetid/TrygdetidService.kt | 6 ------ .../main/kotlin/trygdetid/klienter/PesysKlient.kt | 2 -- .../ktor/ktorobo/DownstreamResourceClient.kt | 15 +++++++++++---- .../src/main/kotlin/ktor/ktorobo/Models.kt | 5 ++++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/TrygdetidService.kt b/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/TrygdetidService.kt index 6bedeb8bb0..2af6c887d1 100644 --- a/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/TrygdetidService.kt +++ b/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/TrygdetidService.kt @@ -1055,12 +1055,6 @@ class ManglerForrigeTrygdetidMaaReguleresManuelt : "Forrige behandling mangler trygdetid, og kan dermed ikke reguleres manuelt", ) -class GrunnlagManglerAvdoede : - UgyldigForespoerselException( - code = "GRUNNLAG_MANGLER_AVDOEDE", - detail = "Ingen avdød(e) funnet i grunnlag. Kan ikke opprette trygdetid.", - ) - class TrygdetidAlleredeOpprettetException : IkkeTillattException("TRYGDETID_FINNES_ALLEREDE", "Det er opprettet trygdetid for behandlingen allerede") diff --git a/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/klienter/PesysKlient.kt b/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/klienter/PesysKlient.kt index d33d576d55..2c798d47f5 100644 --- a/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/klienter/PesysKlient.kt +++ b/apps/etterlatte-trygdetid/src/main/kotlin/trygdetid/klienter/PesysKlient.kt @@ -97,7 +97,6 @@ class PesysKlientImpl( Resource( clientId = clientId, url = "$resourceUrl/api/uforetrygd/grunnlag/trygdetidsgrunnlagListeForLopendeUforetrygd", - ignoreContentType = true, ), brukerTokenInfo = brukerTokenInfo, postBody = TrygdetidsgrunnlagRequest(avdoed.first, avdoed.second.hentDoedsdato()?.verdi!!), @@ -120,7 +119,6 @@ class PesysKlientImpl( Resource( clientId = clientId, url = "$resourceUrl/api/alderspensjon/grunnlag/trygdetidsgrunnlagListeForLopendeAlderspensjon", - ignoreContentType = true, ), brukerTokenInfo = brukerTokenInfo, postBody = TrygdetidsgrunnlagRequest(avdoed.first, avdoed.second.hentDoedsdato()?.verdi!!), diff --git a/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/DownstreamResourceClient.kt b/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/DownstreamResourceClient.kt index 7cb185e2ee..1883fc8dbb 100644 --- a/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/DownstreamResourceClient.kt +++ b/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/DownstreamResourceClient.kt @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode import com.github.michaelbull.result.Ok import com.github.michaelbull.result.andThen import io.ktor.client.HttpClient +import io.ktor.client.call.DoubleReceiveException +import io.ktor.client.call.NoTransformationFoundException import io.ktor.client.call.body import io.ktor.client.request.accept import io.ktor.client.request.bearerAuth @@ -116,6 +118,7 @@ class DownstreamResourceClient( private suspend fun Result.fold(resource: Resource) = this.fold( onSuccess = { response -> + resource.addStatusCode(response.status) when { response.status == HttpStatusCode.NoContent -> Ok(null) response.status.isSuccess() -> { @@ -124,11 +127,15 @@ class DownstreamResourceClient( } else if (response.harContentType(ContentType.Text.Plain)) { Ok(response.body()) } else { - if (resource.ignoreContentType) { + logger.info("Mottok uhåndtert content-type: ${response.contentType()}") + try { Ok(response.body()) - } else { - logger.info("Mottok uhåndtert content-type: ${response.contentType()}") - Ok(response.status) + } catch (e: Exception) { + when (e) { + is NoTransformationFoundException -> Ok(null) + is DoubleReceiveException -> throw e + else -> throw e + } } } } diff --git a/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/Models.kt b/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/Models.kt index 022da64f0c..1ed0de8d71 100644 --- a/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/Models.kt +++ b/libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/Models.kt @@ -6,6 +6,7 @@ import com.github.michaelbull.result.Result import io.ktor.client.call.body import io.ktor.client.plugins.ResponseException import io.ktor.client.statement.HttpResponse +import io.ktor.http.HttpStatusCode import no.nav.etterlatte.libs.common.feilhaandtering.InternfeilException data class Resource( @@ -13,9 +14,11 @@ data class Resource( val url: String, val additionalHeaders: Map? = null, val response: Any? = null, - val ignoreContentType: Boolean = false, + val status: HttpStatusCode? = null, ) { fun addResponse(response: Any?): Resource = this.copy(response = response) + + fun addStatusCode(statusCode: HttpStatusCode): Resource = this.copy(status = statusCode) } data class ThrowableErrorMessage(