From a99e2a9dd9127c4c835a9040c77a1ee196c16f8b Mon Sep 17 00:00:00 2001 From: Allan Galarza Date: Tue, 13 Jun 2023 18:40:03 -0700 Subject: [PATCH] Add specialized REST request exceptions - See #269 --- .../kotlin/request/KtorRequestHandler.kt | 16 +++++++--- .../kotlin/request/RestRequestException.kt | 32 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/rest/src/commonMain/kotlin/request/KtorRequestHandler.kt b/rest/src/commonMain/kotlin/request/KtorRequestHandler.kt index 3ed22ac5e35..aee75b90bc4 100644 --- a/rest/src/commonMain/kotlin/request/KtorRequestHandler.kt +++ b/rest/src/commonMain/kotlin/request/KtorRequestHandler.kt @@ -57,11 +57,17 @@ public class KtorRequestHandler( } response.isError -> { logger.debug { response.logString(body) } - if (response.contentType() == ContentType.Application.Json) - throw KtorRequestException( - response, request, DiscordErrorResponse.serializer().optional.deserialize(parser, body) - ) - else throw KtorRequestException(response, request, null) + val discordError = + if (response.contentType() == ContentType.Application.Json) + DiscordErrorResponse.serializer().optional.deserialize(parser, body) + else null + throw when(response.status){ + HttpStatusCode.BadRequest -> BadRequestKtorRequestException(response, request, discordError) + HttpStatusCode.Forbidden -> ForbiddenKtorRequestException(response, request, discordError) + HttpStatusCode.NotFound -> NotFoundKtorRequestException(response, request, discordError) + HttpStatusCode.InternalServerError -> ServerErrorKtorRequestException(response, request, discordError) + else -> KtorRequestException(response, request, discordError) + } } else -> { logger.debug { response.logString(body) } diff --git a/rest/src/commonMain/kotlin/request/RestRequestException.kt b/rest/src/commonMain/kotlin/request/RestRequestException.kt index f0aa5a6a08c..2fdb7498b8b 100644 --- a/rest/src/commonMain/kotlin/request/RestRequestException.kt +++ b/rest/src/commonMain/kotlin/request/RestRequestException.kt @@ -3,7 +3,7 @@ package dev.kord.rest.request import dev.kord.common.exception.RequestException import dev.kord.rest.json.response.DiscordErrorResponse import dev.kord.rest.service.RestService -import io.ktor.client.statement.HttpResponse +import io.ktor.client.statement.HttpResponse as KtorResponse private fun formatRestRequestExceptionMessage(status: HttpStatus, error: DiscordErrorResponse?): String { val statusCode = status.code @@ -33,8 +33,36 @@ public data class HttpStatus(val code: Int, val message: String) /** * Implementation of the [RestRequestException] for [RestServices][RestService] using Ktor. */ +public open class KtorRequestException( + public val httpResponse: KtorResponse, public class KtorRequestException( - @Suppress("MemberVisibilityCanBePrivate") public val httpResponse: HttpResponse, + @Suppress("MemberVisibilityCanBePrivate") public val httpResponse: KtorResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : RestRequestException(request, HttpStatus(httpResponse.status.value, httpResponse.status.description), discordError) + +public class BadRequestKtorRequestException( + httpResponse: KtorResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + + +public class ForbiddenKtorRequestException( + httpResponse: KtorResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + +public class NotFoundKtorRequestException( + httpResponse: KtorResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + +public class ServerErrorKtorRequestException( + httpResponse: KtorResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) +