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; 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..02036199c51 100644 --- a/rest/src/commonMain/kotlin/request/RestRequestException.kt +++ b/rest/src/commonMain/kotlin/request/RestRequestException.kt @@ -30,11 +30,38 @@ public abstract class RestRequestException( */ public data class HttpStatus(val code: Int, val message: String) + /** * Implementation of the [RestRequestException] for [RestServices][RestService] using Ktor. */ -public class KtorRequestException( +public open class KtorRequestException( @Suppress("MemberVisibilityCanBePrivate") public val httpResponse: HttpResponse, request: Request<*, *>, discordError: DiscordErrorResponse?, ) : RestRequestException(request, HttpStatus(httpResponse.status.value, httpResponse.status.description), discordError) + +public class BadRequestKtorRequestException( + httpResponse: HttpResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + + +public class ForbiddenKtorRequestException( + httpResponse: HttpResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + +public class NotFoundKtorRequestException( + httpResponse: HttpResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) + +public class ServerErrorKtorRequestException( + httpResponse: HttpResponse, + request: Request<*, *>, + discordError: DiscordErrorResponse?, +) : KtorRequestException(httpResponse, request, discordError) +