Skip to content

Commit

Permalink
fix: handle bot companion http client errors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Aug 14, 2024
1 parent 980f010 commit 5f3d6b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class BotCompanionClient(
serverApiPassword: String? = null
): Result<List<Character>> {

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<List<Character>>()))
Expand All @@ -67,7 +71,11 @@ class BotCompanionClient(
serverApiPassword: String? = null
): Result<List<PlayerActivity>> {

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<List<PlayerActivity>>()))
Expand All @@ -82,7 +90,11 @@ class BotCompanionClient(
serverApiPassword: String? = null
): Result<List<PvpKill>> {

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<List<PvpKill>>()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5f3d6b6

Please sign in to comment.