From 5f3d6b60963da0b50e7777a6fec9042ca341883e Mon Sep 17 00:00:00 2001 From: DarkAtra Date: Wed, 14 Aug 2024 19:33:58 +0200 Subject: [PATCH] fix: handle bot companion http client errors correctly --- .../clients/botcompanion/BotCompanionClient.kt | 18 +++++++++++++++--- .../BotCompanionClientException.kt | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClient.kt b/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClient.kt index 079cdcc..ff72253 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClient.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClient.kt @@ -52,7 +52,11 @@ class BotCompanionClient( serverApiPassword: String? = null ): Result> { - val response = performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/characters", serverApiUsername, serverApiPassword) + val response = try { + performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/characters", serverApiUsername, serverApiPassword) + } catch (e: Exception) { + return Result.failure(BotCompanionClientException("Unexpected exception performing ${this::getCharacters.name} request.", e)) + } return when (response.status) { HttpStatusCode.OK -> Result.success(objectMapper.readValue(response.bodyAsText(), jacksonTypeRef>())) @@ -67,7 +71,11 @@ class BotCompanionClient( serverApiPassword: String? = null ): Result> { - val response = performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/player-activities", serverApiUsername, serverApiPassword) + val response = try { + performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/player-activities", serverApiUsername, serverApiPassword) + } catch (e: Exception) { + return Result.failure(BotCompanionClientException("Unexpected exception performing ${this::getPlayerActivities.name} request.", e)) + } return when (response.status) { HttpStatusCode.OK -> Result.success(objectMapper.readValue(response.bodyAsText(), jacksonTypeRef>())) @@ -82,7 +90,11 @@ class BotCompanionClient( serverApiPassword: String? = null ): Result> { - val response = performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/pvp-kills", serverApiUsername, serverApiPassword) + val response = try { + performRequest(getRequestUrl(serverApiHostName, serverApiPort), "/pvp-kills", serverApiUsername, serverApiPassword) + } catch (e: Exception) { + return Result.failure(BotCompanionClientException("Unexpected exception performing ${this::getPvpKills.name} request.", e)) + } return when (response.status) { HttpStatusCode.OK -> Result.success(objectMapper.readValue(response.bodyAsText(), jacksonTypeRef>())) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientException.kt b/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientException.kt index 41c36ea..aa18cf0 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientException.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientException.kt @@ -2,4 +2,4 @@ package de.darkatra.vrising.discord.clients.botcompanion import de.darkatra.vrising.discord.BotException -class BotCompanionClientException(message: String) : BotException(message) +class BotCompanionClientException(message: String, cause: Throwable? = null) : BotException(message, cause)