From a99e2a9dd9127c4c835a9040c77a1ee196c16f8b Mon Sep 17 00:00:00 2001 From: Allan Galarza Date: Tue, 13 Jun 2023 18:40:03 -0700 Subject: [PATCH 1/3] 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) + From bf6b9242695d0f4c5be1c339dbce86546c4f42b6 Mon Sep 17 00:00:00 2001 From: Allan Galarza Date: Tue, 13 Jun 2023 19:08:49 -0700 Subject: [PATCH 2/3] API dump --- rest/api/rest.api | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rest/api/rest.api b/rest/api/rest.api index a63fe2bb2fe..7ea7d50922a 100644 --- a/rest/api/rest.api +++ b/rest/api/rest.api @@ -5969,6 +5969,14 @@ public final class dev/kord/rest/ratelimit/Total { public final synthetic fun unbox-impl ()J } +public final class dev/kord/rest/request/BadRequestKtorRequestException : dev/kord/rest/request/KtorRequestException { + public fun (Lio/ktor/client/statement/HttpResponse;Ldev/kord/rest/request/Request;Ldev/kord/rest/json/response/DiscordErrorResponse;)V +} + +public final class dev/kord/rest/request/ForbiddenKtorRequestException : dev/kord/rest/request/KtorRequestException { + public fun (Lio/ktor/client/statement/HttpResponse;Ldev/kord/rest/request/Request;Ldev/kord/rest/json/response/DiscordErrorResponse;)V +} + public final class dev/kord/rest/request/HttpStatus { public fun (ILjava/lang/String;)V public final fun component1 ()I @@ -6011,7 +6019,7 @@ public final class dev/kord/rest/request/JsonRequest : dev/kord/rest/request/Req public fun getRouteParams ()Ljava/util/Map; } -public final class dev/kord/rest/request/KtorRequestException : dev/kord/rest/request/RestRequestException { +public class dev/kord/rest/request/KtorRequestException : dev/kord/rest/request/RestRequestException { public fun (Lio/ktor/client/statement/HttpResponse;Ldev/kord/rest/request/Request;Ldev/kord/rest/json/response/DiscordErrorResponse;)V public final fun getHttpResponse ()Lio/ktor/client/statement/HttpResponse; } @@ -6042,6 +6050,10 @@ public final class dev/kord/rest/request/MultipartRequest : dev/kord/rest/reques public fun getRouteParams ()Ljava/util/Map; } +public final class dev/kord/rest/request/NotFoundKtorRequestException : dev/kord/rest/request/KtorRequestException { + public fun (Lio/ktor/client/statement/HttpResponse;Ldev/kord/rest/request/Request;Ldev/kord/rest/json/response/DiscordErrorResponse;)V +} + public abstract class dev/kord/rest/request/Request { public synthetic fun (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V @@ -6140,6 +6152,10 @@ public abstract class dev/kord/rest/request/RestRequestException : dev/kord/comm public final fun getStatus ()Ldev/kord/rest/request/HttpStatus; } +public final class dev/kord/rest/request/ServerErrorKtorRequestException : dev/kord/rest/request/KtorRequestException { + public fun (Lio/ktor/client/statement/HttpResponse;Ldev/kord/rest/request/Request;Ldev/kord/rest/json/response/DiscordErrorResponse;)V +} + public final class dev/kord/rest/request/StackTraceRecoveringKtorRequestHandler : dev/kord/rest/request/RequestHandler { public fun (Ldev/kord/rest/request/KtorRequestHandler;)V public fun getToken ()Ljava/lang/String; From b0dc1c47665d48772ca1c7ca06f2bdcffd5dca33 Mon Sep 17 00:00:00 2001 From: Allan Galarza Date: Mon, 19 Jun 2023 15:44:36 -0700 Subject: [PATCH 3/3] Generate apiDump --- .../kotlin/request/RestRequestException.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/rest/src/commonMain/kotlin/request/RestRequestException.kt b/rest/src/commonMain/kotlin/request/RestRequestException.kt index 2fdb7498b8b..02036199c51 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 as KtorResponse +import io.ktor.client.statement.HttpResponse private fun formatRestRequestExceptionMessage(status: HttpStatus, error: DiscordErrorResponse?): String { val statusCode = status.code @@ -30,38 +30,37 @@ public abstract class RestRequestException( */ 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: KtorResponse, + @Suppress("MemberVisibilityCanBePrivate") public val httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : RestRequestException(request, HttpStatus(httpResponse.status.value, httpResponse.status.description), discordError) public class BadRequestKtorRequestException( - httpResponse: KtorResponse, + httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : KtorRequestException(httpResponse, request, discordError) public class ForbiddenKtorRequestException( - httpResponse: KtorResponse, + httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : KtorRequestException(httpResponse, request, discordError) public class NotFoundKtorRequestException( - httpResponse: KtorResponse, + httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : KtorRequestException(httpResponse, request, discordError) public class ServerErrorKtorRequestException( - httpResponse: KtorResponse, + httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : KtorRequestException(httpResponse, request, discordError)