Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specialized REST request exceptions #835

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion rest/api/rest.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (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 <init> (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 <init> (ILjava/lang/String;)V
public final fun component1 ()I
Expand Down Expand Up @@ -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 <init> (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;
}
Expand Down Expand Up @@ -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 <init> (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 <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down Expand Up @@ -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 <init> (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 <init> (Ldev/kord/rest/request/KtorRequestHandler;)V
public fun getToken ()Ljava/lang/String;
Expand Down
16 changes: 11 additions & 5 deletions rest/src/commonMain/kotlin/request/KtorRequestHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
29 changes: 28 additions & 1 deletion rest/src/commonMain/kotlin/request/RestRequestException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)