From 5a642edf2eebda9b3b322cbfce0ebe29ff623c9b Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Thu, 2 Jan 2025 05:18:58 +0100 Subject: [PATCH] update Device Registry - add catch WebClientResponseException to registerDevice in DeviceRegistryService - add handle BAD_REQUEST to WebClient deviceRegistryCreateDevice in WebClientCalls.kt --- src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt | 6 ++++++ src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt index 381c7a0..d4ba554 100644 --- a/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt @@ -6,11 +6,13 @@ import io.micrometer.core.instrument.MeterRegistry import ltd.hlaeja.library.deviceRegistry.Device import ltd.hlaeja.property.DeviceRegistryProperty import ltd.hlaeja.util.deviceRegistryCreateDevice +import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR import org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE import org.springframework.stereotype.Service import org.springframework.web.ErrorResponseException import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClientRequestException +import org.springframework.web.reactive.function.client.WebClientResponseException import org.springframework.web.server.ResponseStatusException private val log = KotlinLogging.logger {} @@ -42,5 +44,9 @@ class DeviceRegistryService( registerDeviceFailure.increment() log.error(e) { "Error device registry" } throw ResponseStatusException(SERVICE_UNAVAILABLE) + } catch (e: WebClientResponseException) { + registerDeviceFailure.increment() + log.error(e) { "Error device registry" } + throw ResponseStatusException(INTERNAL_SERVER_ERROR) } } diff --git a/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt b/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt index 553c0bf..9890c41 100644 --- a/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt +++ b/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt @@ -4,6 +4,7 @@ import ltd.hlaeja.library.accountRegistry.Authentication import ltd.hlaeja.library.deviceRegistry.Device import ltd.hlaeja.property.AccountRegistryProperty import ltd.hlaeja.property.DeviceRegistryProperty +import org.springframework.http.HttpStatus.BAD_REQUEST import org.springframework.http.HttpStatus.LOCKED import org.springframework.http.HttpStatus.NOT_FOUND import org.springframework.http.HttpStatus.REQUEST_TIMEOUT @@ -19,6 +20,7 @@ suspend fun WebClient.deviceRegistryCreateDevice( .uri("${property.url}/device".also(::logCall)) .bodyValue(request) .retrieve() + .onStatus(BAD_REQUEST::equals) { throw ResponseStatusException(BAD_REQUEST) } .awaitBodyOrNull() ?: throw ResponseStatusException(REQUEST_TIMEOUT) suspend fun WebClient.accountRegistryAuthenticate(