diff --git a/rest/src/main/kotlin/request/KtorRequestHandler.kt b/rest/src/main/kotlin/request/KtorRequestHandler.kt index 63fa4115936..510d96672ec 100644 --- a/rest/src/main/kotlin/request/KtorRequestHandler.kt +++ b/rest/src/main/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/main/kotlin/request/RestRequestException.kt b/rest/src/main/kotlin/request/RestRequestException.kt index 49a3c69e64d..9fb11b60fea 100644 --- a/rest/src/main/kotlin/request/RestRequestException.kt +++ b/rest/src/main/kotlin/request/RestRequestException.kt @@ -3,6 +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 { @@ -33,8 +34,34 @@ 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( public val httpResponse: KtorResponse, 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) +