-
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.
Utbedret felles feilhåndtering, la til bekreftelse-api i kafka-keys i…
…nbound
- Loading branch information
1 parent
e4ae96a
commit 166652c
Showing
7 changed files
with
121 additions
and
41 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
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
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
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
12 changes: 12 additions & 0 deletions
12
lib/error-handling/src/main/kotlin/no/nav/paw/error/serialize/HttpStatusCodeDeserializer.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,12 @@ | ||
package no.nav.paw.error.serialize | ||
|
||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonDeserializer | ||
import io.ktor.http.HttpStatusCode | ||
|
||
class HttpStatusCodeDeserializer : JsonDeserializer<HttpStatusCode>() { | ||
override fun deserialize(parser: JsonParser, context: DeserializationContext): HttpStatusCode { | ||
return HttpStatusCode.fromValue(parser.numberValue.toInt()) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/error-handling/src/main/kotlin/no/nav/paw/error/serialize/HttpStatusCodeSerializer.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,12 @@ | ||
package no.nav.paw.error.serialize | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import io.ktor.http.HttpStatusCode | ||
|
||
class HttpStatusCodeSerializer : JsonSerializer<HttpStatusCode>() { | ||
override fun serialize(value: HttpStatusCode, generator: JsonGenerator, provider: SerializerProvider) { | ||
generator.writeNumber(value.value) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
lib/error-handling/src/test/kotlin/no/nav/paw/error/handler/HttpExceptionHandlerTest.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,55 @@ | ||
package no.nav.paw.error.handler | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.get | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.serialization.jackson.jackson | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.plugins.BadRequestException | ||
import io.ktor.server.plugins.statuspages.StatusPages | ||
import io.ktor.server.routing.IgnoreTrailingSlash | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.routing | ||
import io.ktor.server.testing.testApplication | ||
import no.nav.paw.error.model.ProblemDetails | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation as ClientContentNegotiation | ||
import io.ktor.server.application.install as serverInstall | ||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation as ServerContentNegotiation | ||
|
||
class HttpExceptionHandlerTest : FreeSpec({ | ||
"Skal håndtere exceptions og returnere ProblemDetails response" { | ||
testApplication { | ||
application { | ||
serverInstall(IgnoreTrailingSlash) | ||
serverInstall(StatusPages) { | ||
exception<Throwable> { call: ApplicationCall, cause: Throwable -> | ||
call.handleException(cause) | ||
} | ||
} | ||
serverInstall(ServerContentNegotiation) { | ||
jackson {} | ||
} | ||
routing { | ||
get("/api/400") { | ||
throw BadRequestException("It's bad") | ||
} | ||
} | ||
} | ||
|
||
val client = createClient { | ||
install(ClientContentNegotiation) { | ||
jackson {} | ||
} | ||
} | ||
|
||
val response400 = client.get("/api/400") | ||
val responseBody404 = response400.body<ProblemDetails>() | ||
response400.status shouldBe HttpStatusCode.BadRequest | ||
responseBody404.status shouldBe HttpStatusCode.BadRequest | ||
responseBody404.type shouldBe "PAW_KUNNE_IKKE_TOLKE_FORESPOERSEL" | ||
responseBody404.title shouldBe HttpStatusCode.BadRequest.description | ||
} | ||
} | ||
}) |