1
+ package no.nav.paw.error.handler
2
+
3
+ import io.ktor.server.application.ApplicationCall
4
+ import io.ktor.server.plugins.BadRequestException
5
+ import io.ktor.server.plugins.ContentTransformationException
6
+ import io.ktor.server.request.RequestAlreadyConsumedException
7
+ import io.ktor.server.request.uri
8
+ import io.ktor.server.response.respond
9
+ import no.nav.paw.error.exception.ClientResponseException
10
+ import no.nav.paw.error.exception.ServerResponseException
11
+ import no.nav.paw.error.model.build400Error
12
+ import no.nav.paw.error.model.build500Error
13
+ import no.nav.paw.error.model.buildError
14
+ import org.slf4j.Logger
15
+ import org.slf4j.LoggerFactory
16
+
17
+ private const val ERROR_TYPE_PREFIX = " PAW_"
18
+ private val logger: Logger = LoggerFactory .getLogger(" paw.application.error.http" )
19
+
20
+ suspend fun <T : Throwable > ApplicationCall.handleException (throwable : T ) {
21
+ when (throwable) {
22
+ is ContentTransformationException -> {
23
+ val error = build400Error(
24
+ " ${ERROR_TYPE_PREFIX } KUNNE_IKKE_TOLKE_INNHOLD" ,
25
+ " Kunne ikke tolke innhold i kall" ,
26
+ this .request.uri
27
+ )
28
+ logger.debug(error.detail, throwable)
29
+ this .respond(error.status, error)
30
+ }
31
+
32
+ is ClientResponseException -> {
33
+ val error = buildError(
34
+ " ${ERROR_TYPE_PREFIX }${throwable.code} " ,
35
+ throwable.message,
36
+ throwable.status,
37
+ this .request.uri
38
+ )
39
+ logger.warn(error.detail, throwable)
40
+ this .respond(error.status, error)
41
+ }
42
+
43
+ is ServerResponseException -> {
44
+ val error = buildError(
45
+ " ${ERROR_TYPE_PREFIX }${throwable.code} " ,
46
+ throwable.message,
47
+ throwable.status,
48
+ this .request.uri
49
+ )
50
+ logger.error(error.detail, throwable)
51
+ this .respond(error.status, error)
52
+ }
53
+
54
+ is BadRequestException -> {
55
+ val error =
56
+ build400Error(
57
+ " ${ERROR_TYPE_PREFIX } ULOVLIG_FORESPOERSEL" ,
58
+ " Kunne ikke tolke innhold i forespørsel" ,
59
+ this .request.uri
60
+ )
61
+ logger.error(error.detail, throwable)
62
+ this .respond(error.status, error)
63
+ }
64
+
65
+ is RequestAlreadyConsumedException -> {
66
+ val error = build500Error(
67
+ " ${ERROR_TYPE_PREFIX } FORESPOERSEL_ALLEREDE_MOTTATT" ,
68
+ " Forespørsel er allerede mottatt. Dette er en kodefeil" ,
69
+ this .request.uri
70
+ )
71
+ logger.error(error.detail, throwable)
72
+ this .respond(error.status, error)
73
+ }
74
+
75
+ else -> {
76
+ val error = build500Error(
77
+ " ${ERROR_TYPE_PREFIX } UKJENT_FEIL" ,
78
+ " Forespørsel feilet med ukjent feil" ,
79
+ this .request.uri
80
+ )
81
+ logger.error(error.detail, throwable)
82
+ this .respond(error.status, error)
83
+ }
84
+ }
85
+ }
0 commit comments