-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for slash commands option errors (#162)
* Add support for slash commands option errors * nested errors has no definite structure
- Loading branch information
Showing
1 changed file
with
48 additions
and
9 deletions.
There are no files selected for viewing
57 changes: 48 additions & 9 deletions
57
rest/src/main/kotlin/json/response/DiscordErrorResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,72 @@ | ||
package dev.kord.rest.json.response | ||
|
||
import dev.kord.common.entity.optional.Optional | ||
import dev.kord.common.entity.optional.optional | ||
import dev.kord.rest.json.JsonErrorCode | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.builtins.MapSerializer | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.jsonObject | ||
|
||
/** | ||
* Represents a [Discord error response](https://discord.com/developers/docs/topics/opcodes-and-status-codes#json). | ||
*/ | ||
@Serializable | ||
class DiscordErrorResponse( | ||
val code: JsonErrorCode = JsonErrorCode.Unknown, | ||
val errors: Map<String, DiscordFieldError> = emptyMap(), | ||
val message: String = "", | ||
val code: JsonErrorCode = JsonErrorCode.Unknown, | ||
val errors: Map<String, DiscordFieldError> = emptyMap(), | ||
val message: String = "", | ||
) | ||
|
||
/** | ||
* An error for a specific field. | ||
*/ | ||
@Serializable | ||
@Serializable(DiscordFieldError.Serializer::class) | ||
class DiscordFieldError( | ||
@SerialName("_errors") | ||
val errors: List<DiscordErrorDetail>, | ||
) | ||
@SerialName("_errors") | ||
val errors: List<DiscordErrorDetail> = emptyList(), | ||
val nestedErrors: Optional<JsonObject> = Optional.Missing() | ||
) { | ||
internal object Serializer : KSerializer<DiscordFieldError> { | ||
|
||
override val descriptor: SerialDescriptor | ||
get() = JsonObject.serializer().descriptor | ||
|
||
|
||
override fun deserialize(decoder: Decoder): DiscordFieldError { | ||
decoder as JsonDecoder | ||
val json = decoder.decodeJsonElement().jsonObject | ||
// Direct Error message | ||
if (json.containsKey("_errors")) | ||
return DiscordFieldError( | ||
Json.decodeFromJsonElement(ListSerializer(DiscordErrorDetail.serializer()), json["_errors"]!!) | ||
) | ||
|
||
// Nested fields have no definite structure. | ||
return DiscordFieldError(nestedErrors = json.optional()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: DiscordFieldError) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* The detailed code and message for a [DiscordFieldError]. | ||
*/ | ||
@Serializable | ||
class DiscordErrorDetail( | ||
val code: String, | ||
val message: String, | ||
val code: String, | ||
val message: String, | ||
) |