Skip to content

Commit

Permalink
La til feilhåndtering i http client
Browse files Browse the repository at this point in the history
  • Loading branch information
naviktthomas committed Oct 2, 2024
1 parent 312119d commit ffdd6af
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.append
import io.ktor.http.isSuccess
import no.nav.paw.bekreftelse.api.exception.ProblemDetailsException
import no.nav.paw.bekreftelse.api.model.BekreftelseRequest
import no.nav.paw.bekreftelse.api.model.TilgjengeligBekreftelserResponse
import no.nav.paw.bekreftelse.api.model.TilgjengeligeBekreftelserRequest
import no.nav.paw.error.model.ProblemDetails

class BekreftelseHttpConsumer(private val httpClient: HttpClient) {

Expand All @@ -31,12 +28,7 @@ class BekreftelseHttpConsumer(private val httpClient: HttpClient) {
}
setBody(request)
}
if (response.status.isSuccess()) {
return response.body()
} else {
val problemDetails = response.body<ProblemDetails>()
throw ProblemDetailsException(problemDetails)
}
return response.body()
}

suspend fun sendBekreftelse(
Expand All @@ -45,16 +37,12 @@ class BekreftelseHttpConsumer(private val httpClient: HttpClient) {
request: BekreftelseRequest,
) {
val url = "http://$host/api/v1/bekreftelse"
val response = httpClient.post(url) {
httpClient.post(url) {
bearerAuth(bearerToken)
headers {
append(HttpHeaders.ContentType, ContentType.Application.Json)
}
setBody(request)
}
if (response.status.isSuccess()) {
val problemDetails = response.body<ProblemDetails>()
throw ProblemDetailsException(problemDetails)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.paw.bekreftelse.api.context

import io.ktor.client.HttpClient
import io.ktor.client.plugins.HttpResponseValidator
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.jackson.jackson
import io.micrometer.prometheusmetrics.PrometheusConfig
Expand All @@ -16,6 +17,7 @@ import no.nav.paw.bekreftelse.api.services.AuthorizationService
import no.nav.paw.bekreftelse.api.services.BekreftelseService
import no.nav.paw.bekreftelse.api.topology.buildBekreftelseTopology
import no.nav.paw.bekreftelse.api.utils.configureJackson
import no.nav.paw.bekreftelse.api.utils.handleError
import no.nav.paw.config.hoplite.loadNaisOrLocalConfiguration
import no.nav.paw.health.repository.HealthIndicatorRepository
import no.nav.paw.kafkakeygenerator.auth.azureAdM2MTokenClient
Expand Down Expand Up @@ -58,6 +60,9 @@ data class ApplicationContext(
configureJackson()
}
}
HttpResponseValidator {
validateResponse(::handleError)
}
}

val poaoTilgangClient = PoaoTilgangCachedClient(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package no.nav.paw.bekreftelse.api.utils

import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.http.isSuccess
import no.nav.paw.error.exception.ClientResponseException

private fun HttpStatusCode.hasBody(): Boolean {
return this == HttpStatusCode.BadRequest ||
this == HttpStatusCode.Forbidden ||
this == HttpStatusCode.NotFound ||
this == HttpStatusCode.InternalServerError
}

suspend fun handleError(response: HttpResponse) {
if (response.status.isSuccess()) {
return
} else if (response.status.hasBody()) {
val error = response.bodyAsText()
throw ClientResponseException(
HttpStatusCode.InternalServerError,
"PAW_HTTP_KLIENT_KALL_FEILET",
"HTTP-kall feilet med status: ${response.status} og body:\n$error"
)
} else {
throw ClientResponseException(
HttpStatusCode.InternalServerError,
"PAW_HTTP_KLIENT_KALL_FEILET",
"HTTP-kall feilet med status: ${response.status}"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import io.ktor.http.HttpStatusCode
open class AuthenticationException(
override val code: String,
override val message: String,
override val cause: Throwable?
override val cause: Throwable? = null
) : ServerResponseException(HttpStatusCode.Unauthorized, code, message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import io.ktor.http.HttpStatusCode
open class AuthorizationException(
override val code: String,
override val message: String,
override val cause: Throwable?
override val cause: Throwable? = null
) : ServerResponseException(HttpStatusCode.Forbidden, code, message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ open class ClientResponseException(
val status: HttpStatusCode,
override val code: String,
override val message: String,
override val cause: Throwable?
override val cause: Throwable? = null
) : ErrorCodeAwareException(code, message, cause)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nav.paw.error.exception

open class ErrorCodeAwareException(open val code: String, override val message: String, override val cause: Throwable?) :
Exception(message, cause) {

constructor(code: String, message: String) : this(code, message, null)
}
open class ErrorCodeAwareException(
open val code: String,
override val message: String,
override val cause: Throwable? = null
) : Exception(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ open class ServerResponseException(
val status: HttpStatusCode,
override val code: String,
override val message: String,
override val cause: Throwable?
override val cause: Throwable? = null
) : ErrorCodeAwareException(code, message, cause)

0 comments on commit ffdd6af

Please sign in to comment.