From e75d54c547886f569aa896b8f444ced4820eb35d Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Wed, 17 Jan 2024 18:20:38 +0100 Subject: [PATCH] change: Add error handler and log error responses --- .../bahnApi/service/BahnApiService.kt | 16 +++++++++++++--- .../bahnApi/service/BahnErrorHandler.kt | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnErrorHandler.kt diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnApiService.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnApiService.kt index e4989fa..5545a23 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnApiService.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnApiService.kt @@ -6,6 +6,7 @@ import de.twomartens.timetable.bahnApi.model.dto.BahnStations import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable import de.twomartens.timetable.bahnApi.property.BahnApiProperties import de.twomartens.timetable.types.HourAtDay +import mu.KotlinLogging import org.springframework.http.MediaType import org.springframework.stereotype.Service import org.springframework.web.client.RestClient @@ -35,9 +36,12 @@ class BahnApiService( val dateFormatter = DateTimeFormatter.ofPattern("yyMMdd") val timeFormatter = DateTimeFormatter.ofPattern("HH") val time = LocalTime.of(hourAtDay.hour.value, 0) - val body = restClient.get() + val day = hourAtDay.date.format(dateFormatter) + val hour = time.format(timeFormatter) + val bahnErrorHandler = BahnErrorHandler() + val response = restClient.get() .uri("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/" + - "${eva.value}/${hourAtDay.date.format(dateFormatter)}/${time.format(timeFormatter)}") + "${eva.value}/${day}/${hour}") .headers { it.accept = mutableListOf(MediaType.APPLICATION_XML) it.contentType = MediaType.APPLICATION_XML @@ -45,8 +49,14 @@ class BahnApiService( it.set("DB-Api-Key", properties.clientSecret) } .retrieve() - .body(BahnTimetable::class.java) + .onStatus(bahnErrorHandler) + log.debug { "Response from DB API, station [${eva.value}], date [$day], hour [$hour]" } + val body = response.body(BahnTimetable::class.java) body!!.eva = eva return body } + + companion object { + private val log = KotlinLogging.logger {} + } } \ No newline at end of file diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnErrorHandler.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnErrorHandler.kt new file mode 100644 index 0000000..7c046e6 --- /dev/null +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnErrorHandler.kt @@ -0,0 +1,19 @@ +package de.twomartens.timetable.bahnApi.service + +import mu.KotlinLogging +import org.springframework.http.client.ClientHttpResponse +import org.springframework.web.client.ResponseErrorHandler + +class BahnErrorHandler : ResponseErrorHandler { + override fun hasError(response: ClientHttpResponse): Boolean { + return response.statusCode.isError + } + + override fun handleError(response: ClientHttpResponse) { + log.debug { "Error response: $response" } + } + + companion object { + private val log = KotlinLogging.logger {} + } +} \ No newline at end of file