Skip to content

Commit

Permalink
Add specialized REST request exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarzaa90 committed Jun 19, 2023
1 parent b63b009 commit a99e2a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
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
32 changes: 30 additions & 2 deletions rest/src/commonMain/kotlin/request/RestRequestException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit a99e2a9

Please sign in to comment.