-
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.
Merge pull request #89 from navikt/oppgradering-ktor3-bekreftelse-api
oppgraderer bekreftelse-api til ktor3
- Loading branch information
Showing
45 changed files
with
1,150 additions
and
57 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
dependencies { | ||
compileOnly(libs.ktor3.server.cors) | ||
compileOnly(libs.ktor3.serialization.jackson) | ||
compileOnly(libs.kafka.streams.core) | ||
compileOnly(libs.logbackClassic) | ||
|
||
//Test | ||
testImplementation(libs.bundles.testLibsWithUnitTesting) | ||
testImplementation(libs.ktor3.server.test.host) | ||
testImplementation(libs.ktor3.server.contentNegotiation) | ||
testImplementation(libs.ktor3.server.statusPages) | ||
testImplementation(libs.ktor3.serialization.jackson) | ||
testImplementation(libs.ktor3.client.contentNegotiation) | ||
testImplementation(libs.ktor3.server.core) | ||
testImplementation(libs.kafka.streams.core) | ||
testImplementation(libs.jackson.datatypeJsr310) | ||
testImplementation(libs.logbackClassic) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
useJUnitPlatform() | ||
} |
11 changes: 11 additions & 0 deletions
11
...rror-handling-ktor3/src/main/kotlin/no/nav/paw/error/exception/ClientResponseException.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,11 @@ | ||
package no.nav.paw.error.exception | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import java.net.URI | ||
|
||
open class ClientResponseException( | ||
val status: HttpStatusCode, | ||
override val type: URI, | ||
override val message: String, | ||
override val cause: Throwable? = null | ||
) : ErrorTypeAwareException(type, message, cause) |
11 changes: 11 additions & 0 deletions
11
...rror-handling-ktor3/src/main/kotlin/no/nav/paw/error/exception/ErrorTypeAwareException.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,11 @@ | ||
package no.nav.paw.error.exception | ||
|
||
import java.net.URI | ||
|
||
open class ErrorTypeAwareException( | ||
open val type: URI, | ||
override val message: String, | ||
override val cause: Throwable? = null | ||
) : | ||
Exception(message, cause) { | ||
} |
8 changes: 8 additions & 0 deletions
8
...rror-handling-ktor3/src/main/kotlin/no/nav/paw/error/exception/ProblemDetailsException.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,8 @@ | ||
package no.nav.paw.error.exception | ||
|
||
import no.nav.paw.error.model.ProblemDetails | ||
|
||
open class ProblemDetailsException(val details: ProblemDetails): ErrorTypeAwareException( | ||
type = details.type, | ||
message = details.title | ||
) |
11 changes: 11 additions & 0 deletions
11
...rror-handling-ktor3/src/main/kotlin/no/nav/paw/error/exception/ServerResponseException.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,11 @@ | ||
package no.nav.paw.error.exception | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import java.net.URI | ||
|
||
open class ServerResponseException( | ||
val status: HttpStatusCode, | ||
override val type: URI, | ||
override val message: String, | ||
override val cause: Throwable? = null | ||
) : ErrorTypeAwareException(type, message, cause) |
107 changes: 107 additions & 0 deletions
107
lib/error-handling-ktor3/src/main/kotlin/no/nav/paw/error/handler/HttpExceptionHandler.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,107 @@ | ||
package no.nav.paw.error.handler | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.plugins.BadRequestException | ||
import io.ktor.server.plugins.ContentTransformationException | ||
import io.ktor.server.request.ApplicationRequest | ||
import io.ktor.server.request.RequestAlreadyConsumedException | ||
import io.ktor.server.request.uri | ||
import io.ktor.server.response.respond | ||
import no.nav.paw.error.exception.ClientResponseException | ||
import no.nav.paw.error.exception.ServerResponseException | ||
import no.nav.paw.error.model.ErrorType | ||
import no.nav.paw.error.model.ProblemDetails | ||
import no.nav.paw.error.model.ProblemDetailsBuilder | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.slf4j.MDC | ||
|
||
private val logger: Logger = LoggerFactory.getLogger("no.nav.paw.logger.error.http") | ||
private const val MDC_ERROR_ID_KEY = "x_error_id" | ||
private const val MDC_ERROR_TYPE_KEY = "x_error_type" | ||
private const val MDC_EXCEPTION_KEY = "exception" | ||
|
||
suspend fun ApplicationCall.handleException( | ||
throwable: Throwable, | ||
resolver: (throwable: Throwable) -> ProblemDetails? = { null } | ||
) { | ||
val problemDetails = resolveProblemDetails(request, throwable, resolver) | ||
|
||
MDC.put(MDC_ERROR_ID_KEY, problemDetails.id.toString()) | ||
MDC.put(MDC_ERROR_TYPE_KEY, problemDetails.type.toString()) | ||
MDC.put(MDC_EXCEPTION_KEY, throwable.javaClass.canonicalName) | ||
|
||
logger.error(problemDetails.detail, throwable) | ||
|
||
MDC.remove(MDC_ERROR_ID_KEY) | ||
MDC.remove(MDC_ERROR_TYPE_KEY) | ||
MDC.remove(MDC_EXCEPTION_KEY) | ||
|
||
respond(problemDetails.status, problemDetails) | ||
} | ||
|
||
fun resolveProblemDetails( | ||
request: ApplicationRequest, | ||
throwable: Throwable, | ||
resolver: (throwable: Throwable) -> ProblemDetails? = { null } | ||
): ProblemDetails { | ||
val problemDetails = resolver(throwable) | ||
if (problemDetails != null) { | ||
return problemDetails | ||
} | ||
|
||
when (throwable) { | ||
is BadRequestException -> { | ||
return ProblemDetailsBuilder.builder() | ||
.type(ErrorType.domain("http").error("kunne-ikke-tolke-forespoersel").build()) | ||
.status(HttpStatusCode.BadRequest) | ||
.detail("Kunne ikke tolke forespørsel") | ||
.instance(request.uri) | ||
.build() | ||
} | ||
|
||
is ContentTransformationException -> { | ||
return ProblemDetailsBuilder.builder() | ||
.type(ErrorType.domain("http").error("kunne-ikke-tolke-innhold").build()) | ||
.status(HttpStatusCode.BadRequest) | ||
.detail("Kunne ikke tolke innhold i forespørsel") | ||
.instance(request.uri) | ||
.build() | ||
} | ||
|
||
is RequestAlreadyConsumedException -> { | ||
return ProblemDetailsBuilder.builder() | ||
.type(ErrorType.domain("http").error("forespoersel-allerede-mottatt").build()) | ||
.status(HttpStatusCode.InternalServerError) | ||
.detail("Forespørsel er allerede mottatt. Dette er en kodefeil") | ||
.instance(request.uri) | ||
.build() | ||
} | ||
|
||
is ServerResponseException -> { | ||
return ProblemDetailsBuilder.builder() | ||
.type(throwable.type) | ||
.status(throwable.status) | ||
.detail(throwable.message) | ||
.instance(request.uri) | ||
.build() | ||
} | ||
|
||
is ClientResponseException -> { | ||
return ProblemDetailsBuilder.builder() | ||
.type(throwable.type) | ||
.status(throwable.status) | ||
.detail(throwable.message) | ||
.instance(request.uri) | ||
.build() | ||
} | ||
|
||
else -> { | ||
return ProblemDetailsBuilder.builder() | ||
.detail("Forespørsel feilet med ukjent feil") | ||
.instance(request.uri) | ||
.build() | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
lib/error-handling-ktor3/src/main/kotlin/no/nav/paw/error/handler/KafkaExceptionHandler.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,17 @@ | ||
package no.nav.paw.error.handler | ||
|
||
import org.apache.kafka.streams.KafkaStreams | ||
import org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
private val logger: Logger = LoggerFactory.getLogger("no.nav.paw.logger.error.kafka") | ||
|
||
fun KafkaStreams.withApplicationTerminatingExceptionHandler() { | ||
this.setUncaughtExceptionHandler(createApplicationTerminatingExceptionHandler()) | ||
} | ||
|
||
fun createApplicationTerminatingExceptionHandler() = StreamsUncaughtExceptionHandler { throwable -> | ||
logger.error("Kafka Streams opplevde en uventet feil", throwable) | ||
StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_APPLICATION | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/error-handling-ktor3/src/main/kotlin/no/nav/paw/error/model/ErrorType.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,27 @@ | ||
package no.nav.paw.error.model | ||
|
||
import java.net.URI | ||
|
||
object ErrorTypeDefaults { | ||
const val TEAM = "paw" | ||
const val DOMAIN = "default" | ||
const val ERROR = "ukjent-feil" | ||
} | ||
|
||
class ErrorType( | ||
var team: String = ErrorTypeDefaults.TEAM, | ||
var domain: String = ErrorTypeDefaults.DOMAIN, | ||
var error: String = ErrorTypeDefaults.ERROR, | ||
) { | ||
fun team(team: String) = apply { this.team = team } | ||
fun domain(domain: String) = apply { this.domain = domain } | ||
fun error(error: String) = apply { this.error = error } | ||
fun build(): URI = URI.create("urn:${team.lowercase()}:${domain.lowercase()}:${error.lowercase()}") | ||
|
||
companion object { | ||
fun team(team: String): ErrorType = ErrorType(team = team) | ||
fun domain(domain: String): ErrorType = ErrorType(domain = domain) | ||
fun error(error: String): ErrorType = ErrorType(error = error) | ||
fun default(): ErrorType = ErrorType() | ||
} | ||
} |
Oops, something went wrong.