Skip to content

Commit

Permalink
Add locale and bundle to ArgumentParsingException
Browse files Browse the repository at this point in the history
Fixes #214
  • Loading branch information
gdude2002 committed Oct 27, 2023
1 parent c82e73c commit 8cc21e9
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.kotlindiscord.kord.extensions.commands.converters.builders.ConverterB
import com.kotlindiscord.kord.extensions.events.EventHandler
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.parser.StringParser
import java.util.Locale
import kotlin.reflect.KClass

/**
Expand All @@ -27,13 +28,13 @@ public open class ExtensionsException : Exception()
* @property reason Reason for the validation failure
**/
public class InvalidArgumentException(
public val builder: ConverterBuilder<*>,
public val reason: String
public val builder: ConverterBuilder<*>,
public val reason: String,
) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String =
"Invalid argument: $builder ($reason)"
override fun toString(): String =
"Invalid argument: $builder ($reason)"
}

/**
Expand All @@ -43,20 +44,20 @@ public class InvalidArgumentException(
* @param reason Why this [Extension] is considered invalid.
*/
public class InvalidExtensionException(
public val clazz: KClass<out Extension>,
public val reason: String?
public val clazz: KClass<out Extension>,
public val reason: String?,
) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String {
val formattedReason = if (reason != null) {
" ($reason)"
} else {
""
}
override fun toString(): String {
val formattedReason = if (reason != null) {
" ($reason)"
} else {
""
}

return "Invalid extension class: ${clazz.qualifiedName}$formattedReason"
}
return "Invalid extension class: ${clazz.qualifiedName}$formattedReason"
}
}

/**
Expand All @@ -65,9 +66,9 @@ public class InvalidExtensionException(
* @param reason Why this [EventHandler] is considered invalid.
*/
public class InvalidEventHandlerException(public val reason: String) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String = "Invalid event handler: $reason"
override fun toString(): String = "Invalid event handler: $reason"
}

/**
Expand All @@ -76,9 +77,9 @@ public class InvalidEventHandlerException(public val reason: String) : Extension
* @param reason Why this [EventHandler] could not be registered.
*/
public class EventHandlerRegistrationException(public val reason: String) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String = "Failed to register event handler: $reason"
override fun toString(): String = "Failed to register event handler: $reason"
}

/**
Expand All @@ -88,15 +89,15 @@ public class EventHandlerRegistrationException(public val reason: String) : Exte
* @param reason Why this [ChatCommand] is considered invalid.
*/
public class InvalidCommandException(public val name: String?, public val reason: String) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String {
if (name == null) {
return "Invalid command: $reason"
}
override fun toString(): String {
if (name == null) {
return "Invalid command: $reason"
}

return "Invalid command $name: $reason"
}
return "Invalid command $name: $reason"
}
}

/**
Expand All @@ -106,15 +107,15 @@ public class InvalidCommandException(public val name: String?, public val reason
* @param reason Why this [ChatCommand] could not be registered.
*/
public class CommandRegistrationException(public val name: String?, public val reason: String) : ExtensionsException() {
override val message: String = toString()
override val message: String = toString()

override fun toString(): String {
if (name == null) {
return "Failed to register command: $reason"
}
override fun toString(): String {
if (name == null) {
return "Failed to register command: $reason"
}

return "Failed to register command $name: $reason"
}
return "Failed to register command $name: $reason"
}
}

/**
Expand All @@ -126,14 +127,14 @@ public class CommandRegistrationException(public val name: String?, public val r
* @param translationKey Translation key used to create the [reason] string, if any.
*/
public open class DiscordRelayedException(
public open val reason: String,
public open val translationKey: String? = null
public open val reason: String,
public open val translationKey: String? = null,
) : ExtensionsException() {
override val message: String by lazy { toString() }
override val message: String by lazy { toString() }

public constructor(other: DiscordRelayedException) : this(other.reason)
public constructor(other: DiscordRelayedException) : this(other.reason)

override fun toString(): String = reason
override fun toString(): String = reason
}

/**
Expand All @@ -146,16 +147,23 @@ public open class DiscordRelayedException(
* @param parser Tokenizing string parser used for this parse attempt, if this was a chat command.
*/
public open class ArgumentParsingException(
public override val reason: String,
public override val translationKey: String?,
public val argument: Argument<*>?,
public val arguments: Arguments,
public val parser: StringParser?
public override val reason: String,
public override val translationKey: String?,
public val locale: Locale,
public val bundle: String?,
public val argument: Argument<*>?,
public val arguments: Arguments,
public val parser: StringParser?,
) : DiscordRelayedException(reason, translationKey) {
override val message: String by lazy { toString() }
override val message: String by lazy { toString() }

public constructor(other: ArgumentParsingException) :
this(other.reason, other.translationKey, other.argument, other.arguments, other.parser)
public constructor(other: ArgumentParsingException) :
this(
other.reason,
other.translationKey, other.locale, other.bundle,
other.argument, other.arguments,
other.parser
)

override fun toString(): String = reason
override fun toString(): String = reason
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public open class SlashCommandParser {

"argumentParser.error.invalidValue",

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand All @@ -120,6 +123,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down Expand Up @@ -157,6 +163,9 @@ public open class SlashCommandParser {

"argumentParser.error.invalidValue",

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand All @@ -177,6 +186,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down Expand Up @@ -211,6 +223,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down Expand Up @@ -245,6 +260,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down Expand Up @@ -279,6 +297,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down Expand Up @@ -313,6 +334,9 @@ public open class SlashCommandParser {
converter.handleError(e, context),
null,

context.getLocale(),
context.command.resolvedBundle ?: converter.bundle,

currentArg,
argumentsObj,
null
Expand Down
Loading

0 comments on commit 8cc21e9

Please sign in to comment.