-
Notifications
You must be signed in to change notification settings - Fork 81
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
base: main
Are you sure you want to change the base?
Conversation
you need to run |
Done |
The request handler is about to get deprecated, instead it would be great to use ktors built in response validation feature |
I agree. On the exceptions side, what do you guys think of the naming? Also, should I stick to these? https://discord.com/developers/docs/topics/opcodes-and-status-codes#http-http-response-codes |
Sounds great, as making an exception class for every single json error code might be a bit too much, however we might be able to generate those /cc @lukellmann |
I definitely think we should use Ktor's response validation. However, I was checking kord's code and it would mean we have to add This is a bit more entangled into the code 🤔 |
Kord core should use the same client for everything, right? |
You're right. I had another go at it. I was trying to add the validation to internal fun HttpClient?.configure(): HttpClient {
if (this != null) return this.config {
defaultConfig()
}
val json = Json {
encodeDefaults = false
allowStructuredMapKeys = true
ignoreUnknownKeys = true
isLenient = true
}
return HttpClient(CIO) {
defaultConfig()
install(ContentNegotiation) {
json(json)
}
expectSuccess = true
HttpResponseValidator {
handleResponseExceptionWithRequest { exception, request ->
val clientException = exception as? ResponseException ?: return@handleResponseExceptionWithRequest
val response = clientException.response
if(response.isRateLimit) return@handleResponseExceptionWithRequest
val body = response.bodyAsText()
val discordError = if (response.isError) {
if (response.contentType() == ContentType.Application.Json)
DiscordErrorResponse.serializer().optional.deserialize(json, body)
else null
} 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)
}
}
}
}
} |
I noticed I created this fork from |
i don't think it's worth having an exception per json error code (tbh i'm not even sure if the json error code enum is even useful) |
yeah, that's because some commits got cherrypicked from one to the other but there was no merge between them, so they diverged if you merge 0.9.x into the branch of your pr, it should be alright |
or to make the history nicer, you can drop the commits before your work from the branch (e.g. with interactive rebase) and then rebase onto 0.9.x |
Ok, I finally fixed the history. My branch is basically I'm still dealing with this conundrum though: |
(See #269)
This is a simple attempt at implementing specialized REST request exceptions.
I initially considered making
KtorRequestException
a sealed class and making subclasses for the errors, but this would mean any implementation creating instances ofKtorRequestException
would be broken, and a catch-all subclass would be needed to handle all other cases.I'm opening this as a draft as this is just a simple proposal that needs to be polished based on feedback received.
What is the purpose of this?
Going from this:
To this: