Skip to content

Commit

Permalink
Håndter response 200 bedre
Browse files Browse the repository at this point in the history
  • Loading branch information
sebassonav committed Dec 19, 2024
1 parent 6d53484 commit d4fa96c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!!),
Expand All @@ -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!!),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,6 +118,7 @@ class DownstreamResourceClient(
private suspend fun Result<HttpResponse>.fold(resource: Resource) =
this.fold(
onSuccess = { response ->
resource.addStatusCode(response.status)
when {
response.status == HttpStatusCode.NoContent -> Ok(null)
response.status.isSuccess() -> {
Expand All @@ -124,11 +127,15 @@ class DownstreamResourceClient(
} else if (response.harContentType(ContentType.Text.Plain)) {
Ok(response.body<String>())
} else {
if (resource.ignoreContentType) {
logger.info("Mottok uhåndtert content-type: ${response.contentType()}")
try {
Ok(response.body<String>())
} 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
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion libs/etterlatte-ktor/src/main/kotlin/ktor/ktorobo/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ 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(
val clientId: String,
val url: String,
val additionalHeaders: Map<String, String>? = 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(
Expand Down

0 comments on commit d4fa96c

Please sign in to comment.