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 14, 2023
1 parent 1d7ec55 commit bdb76fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 11 additions & 5 deletions rest/src/main/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/main/kotlin/request/RestRequestException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

0 comments on commit bdb76fc

Please sign in to comment.