Skip to content

Commit dfe8605

Browse files
committed
Innført bruk av ErrorHandlingPlugin
1 parent 9edcc1a commit dfe8605

File tree

18 files changed

+288
-157
lines changed

18 files changed

+288
-157
lines changed

apps/bekreftelse-hendelsefilter/src/main/kotlin/no/nav/paw/bekreftelse/utils/KafkaSerialization.kt

-6
This file was deleted.

apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/Application.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ fun startApplication(
105105
})
106106
}
107107
) {
108+
configureErrorHandling()
108109
configSerialization()
109110
configureLogging()
110-
configureErrorHandling()
111111
configureAuthentication(authenticationConfig)
112112
configureMetrics(
113113
meterRegistry = prometheusMeterRegistry,
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,9 @@
11
package no.nav.paw.kafkakeygenerator.plugin
22

3-
import com.fasterxml.jackson.databind.DatabindException
4-
import io.ktor.http.ContentType
5-
import io.ktor.http.HttpStatusCode
63
import io.ktor.server.application.Application
74
import io.ktor.server.application.install
8-
import io.ktor.server.plugins.statuspages.StatusPages
9-
import io.ktor.server.request.path
10-
import io.ktor.server.response.respondText
11-
import no.nav.paw.kafkakeygenerator.utils.masker
12-
import org.slf4j.LoggerFactory
13-
14-
private val feilLogger = LoggerFactory.getLogger("error_logger")
5+
import no.nav.paw.error.plugin.ErrorHandlingPlugin
156

167
fun Application.configureErrorHandling() {
17-
install(StatusPages) {
18-
exception<Throwable> { call, throwable ->
19-
when (throwable) {
20-
is DatabindException -> {
21-
feilLogger.info(
22-
"Ugyldig kall {}, feilet, grunnet: {}",
23-
masker(call.request.path()),
24-
masker(throwable.message)
25-
)
26-
call.respondText(
27-
"Bad request",
28-
ContentType.Text.Plain,
29-
HttpStatusCode.BadRequest
30-
)
31-
}
32-
33-
else -> {
34-
feilLogger.error(
35-
"Kall {}, feilet, grunnet: {}",
36-
masker(call.request.path()),
37-
masker(throwable.message)
38-
)
39-
call.respondText(
40-
"En uventet feil oppstod",
41-
ContentType.Text.Plain,
42-
HttpStatusCode.InternalServerError
43-
)
44-
}
45-
}
46-
}
47-
}
8+
install(ErrorHandlingPlugin)
489
}

apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/plugin/Routing.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fun Application.configureRouting(
2626
prometheusMeterRegistry = prometheusMeterRegistry,
2727
mergeDetector = mergeDetector
2828
)
29-
konfigurerApiV2(authenticationConfig, kafkaKeysService)
30-
configureRecordKeyApi(authenticationConfig, kafkaKeysService)
3129
swaggerUI(path = "docs", swaggerFile = "openapi/documentation.yaml")
3230
swaggerUI(path = "docs/record-key", swaggerFile = "openapi/record-key-api-spec.yaml")
31+
konfigurerApiV2(authenticationConfig, kafkaKeysService)
32+
configureRecordKeyApi(authenticationConfig, kafkaKeysService)
3333
}
3434
}

apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/plugin/Serialization.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import io.ktor.serialization.jackson.jackson
44
import io.ktor.server.application.Application
55
import io.ktor.server.application.install
66
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
7+
import no.nav.paw.kafkakeygenerator.utils.configureJackson
78

89
fun Application.configSerialization() {
910
install(ContentNegotiation) {
10-
jackson()
11+
jackson {
12+
configureJackson()
13+
}
1114
}
1215
}

apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/utils/IdMaskering.kt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package no.nav.paw.kafkakeygenerator.utils
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude
4+
import com.fasterxml.jackson.databind.DeserializationFeature
5+
import com.fasterxml.jackson.databind.ObjectMapper
6+
import com.fasterxml.jackson.databind.SerializationFeature
7+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
8+
import com.fasterxml.jackson.module.kotlin.KotlinFeature
9+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
10+
import com.fasterxml.jackson.module.kotlin.kotlinModule
11+
12+
val buildObjectMapper: ObjectMapper
13+
get() = jacksonObjectMapper().apply {
14+
configureJackson()
15+
}
16+
17+
fun ObjectMapper.configureJackson() {
18+
setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
19+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
20+
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
21+
disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
22+
registerModule(JavaTimeModule())
23+
kotlinModule {
24+
withReflectionCacheSize(512)
25+
disable(KotlinFeature.NullIsSameAsDefault)
26+
disable(KotlinFeature.SingletonSupport)
27+
disable(KotlinFeature.StrictNullChecks)
28+
enable(KotlinFeature.NullToEmptyCollection)
29+
enable(KotlinFeature.NullToEmptyMap)
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
host = "localhost"
22
port = 5432
33
database = "pawkafkakeys"
4-
username = "admin"
5-
password = "admin"
4+
username = "kafka_key_generator"
5+
password = "Paw1234"
66
driverClassName = "org.postgresql.Driver"
77
autoCommit = false

apps/kafka-key-generator/src/main/resources/openapi/documentation.yaml

+144-42
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ servers:
88
paths:
99
/api/v2/info:
1010
post:
11-
description: ""
11+
operationId: postHentInfoV2
12+
description: "Hent info"
1213
parameters:
1314
- name: "traceparent"
1415
in: "header"
@@ -17,44 +18,49 @@ paths:
1718
type: "string"
1819
requestBody:
1920
content:
20-
'*/*':
21+
application/json:
2122
schema:
2223
$ref: "#/components/schemas/RequestV2"
2324
required: true
2425
responses:
26+
"200":
27+
description: "OK"
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/InfoResponse"
2532
"400":
2633
description: "Bad Request"
2734
content:
28-
text/plain:
35+
application/json:
2936
schema:
30-
type: "string"
31-
examples:
32-
Example#1:
33-
value: "Bad request"
37+
$ref: "#/components/schemas/ProblemDetails"
38+
example:
39+
id: "3cd944fb-6187-41a8-91b2-b172f2baf890"
40+
type: "urn:paw:http:kunne-ikke-tolke-forespoersel"
41+
status: 400
42+
title: "Bad Request"
43+
detail: "Kunne ikke tolke forespørsel"
44+
instance: "/api/v2/info"
45+
timestamp: "2021-01-01T12:00:00.000Z"
3446
"500":
3547
description: "Internal Server Error"
3648
content:
37-
text/plain:
49+
application/json:
3850
schema:
39-
type: "string"
40-
examples:
41-
Example#1:
42-
value: "En uventet feil oppstod"
43-
"200":
44-
description: ""
45-
content:
46-
text/plain:
47-
schema:
48-
type: "string"
49-
examples:
50-
Example#1:
51-
value: "Intern feil, prøv igjen senere"
52-
'*/*':
53-
schema:
54-
$ref: "#/components/schemas/InfoResponse"
51+
$ref: "#/components/schemas/ProblemDetails"
52+
example:
53+
id: "3cd944fb-6187-41a8-91b2-b172f2baf890"
54+
type: "urn:paw:default:ukjent-feil"
55+
status: 500
56+
title: "Internal Server Error"
57+
detail: "Ukjent feil"
58+
instance: "/api/v2/info"
59+
timestamp: "2021-01-01T12:00:00.000Z"
5560
/api/v2/hentEllerOpprett:
5661
post:
57-
description: ""
62+
operationId: postHentEllerOpprettKeyV2
63+
description: "Hent eller opprett Kafka keys"
5864
parameters:
5965
- name: "traceparent"
6066
in: "header"
@@ -63,41 +69,90 @@ paths:
6369
type: "string"
6470
requestBody:
6571
content:
66-
'*/*':
72+
application/json:
6773
schema:
6874
$ref: "#/components/schemas/RequestV2"
6975
required: true
7076
responses:
77+
"200":
78+
description: "OK"
79+
content:
80+
application/json:
81+
schema:
82+
$ref: "#/components/schemas/ResponseV2"
7183
"400":
7284
description: "Bad Request"
7385
content:
74-
text/plain:
86+
application/json:
7587
schema:
76-
type: "string"
77-
examples:
78-
Example#1:
79-
value: "Bad request"
88+
$ref: "#/components/schemas/ProblemDetails"
89+
example:
90+
id: "3cd944fb-6187-41a8-91b2-b172f2baf890"
91+
type: "urn:paw:http:kunne-ikke-tolke-forespoersel"
92+
status: 400
93+
title: "Bad Request"
94+
detail: "Kunne ikke tolke forespørsel"
95+
instance: "/api/v2/hentEllerOpprett"
96+
timestamp: "2021-01-01T12:00:00.000Z"
8097
"500":
8198
description: "Internal Server Error"
99+
content:
100+
application/json:
101+
schema:
102+
$ref: "#/components/schemas/ProblemDetails"
103+
example:
104+
id: "3cd944fb-6187-41a8-91b2-b172f2baf890"
105+
type: "urn:paw:default:ukjent-feil"
106+
status: 500
107+
title: "Internal Server Error"
108+
detail: "Ukjent feil"
109+
instance: "/api/v2/hentEllerOpprett"
110+
timestamp: "2021-01-01T12:00:00.000Z"
111+
/internal/isAlive:
112+
get:
113+
operationId: getIsAlive
114+
description: "Service is alive probe"
115+
responses:
116+
"200":
117+
description: "OK"
82118
content:
83119
text/plain:
84120
schema:
85-
type: "string"
86-
examples:
87-
Example#1:
88-
value: "En uventet feil oppstod"
121+
$ref: "#/components/schemas/HealthStatus"
122+
"503":
123+
description: "Service Unavailable"
124+
content:
125+
text/plain:
126+
schema:
127+
$ref: "#/components/schemas/HealthStatus"
128+
/internal/isReady:
129+
get:
130+
operationId: getIsReady
131+
description: "Service is ready probe"
132+
responses:
89133
"200":
90-
description: ""
134+
description: "OK"
91135
content:
92136
text/plain:
93137
schema:
94-
type: "string"
95-
examples:
96-
Example#1:
97-
value: "Intern feil, prøv igjen senere"
98-
'*/*':
138+
$ref: "#/components/schemas/HealthStatus"
139+
"503":
140+
description: "Service Unavailable"
141+
content:
142+
text/plain:
99143
schema:
100-
$ref: "#/components/schemas/ResponseV2"
144+
$ref: "#/components/schemas/HealthStatus"
145+
/internal/metrics:
146+
get:
147+
operationId: getMetrics
148+
description: "Prometheus metrics"
149+
responses:
150+
"200":
151+
description: "OK"
152+
content:
153+
application/json:
154+
schema:
155+
type: "string"
101156
components:
102157
schemas:
103158
InfoResponse:
@@ -146,6 +201,8 @@ components:
146201
type: "string"
147202
required:
148203
- "ident"
204+
example:
205+
ident: "01017012345"
149206
ResponseV2:
150207
type: "object"
151208
properties:
@@ -155,3 +212,48 @@ components:
155212
key:
156213
type: "integer"
157214
format: "int64"
215+
example:
216+
id: 1234
217+
key: -1234
218+
ProblemDetails:
219+
type: object
220+
properties:
221+
id:
222+
type: "string"
223+
format: "uuid"
224+
type:
225+
type: "string"
226+
format: "uri"
227+
status:
228+
type: "integer"
229+
title:
230+
type: "string"
231+
detail:
232+
type: "string"
233+
instance:
234+
type: "string"
235+
timestamp:
236+
type: "string"
237+
format: "date-time"
238+
required:
239+
- id
240+
- type
241+
- status
242+
- title
243+
- detail
244+
- instance
245+
- timestamp
246+
example:
247+
id: "3cd944fb-6187-41a8-91b2-b172f2baf890"
248+
type: "urn:paw:default:ukjent-feil"
249+
status: 500
250+
title: "Internal Server Error"
251+
detail: "Ukjent feil"
252+
instance: "/api/v2/info"
253+
timestamp: "2021-01-01T12:00:00.000Z"
254+
HealthStatus:
255+
type: "string"
256+
enum:
257+
- "UNKNOWN"
258+
- "HEALTHY"
259+
- "UNHEALTHY"

0 commit comments

Comments
 (0)