Kord-Extensions / kord-extensions

Kord extensions framework, providing commands and distinct units of functionality
https://kordex.dev
European Union Public License 1.2
112 stars 27 forks source link

Adding Context or Locale & Bundle to ArgumentParsingException #214

Closed Galarzaa90 closed 1 year ago

Galarzaa90 commented 1 year ago

Summary

Argument parsing error messages lack information, like which argument is incorrect.

Current Behaviour

The standard way to add validation to command arguments is using the failIf function. Providing localized error is easy since this is within a ValidationContext:

validate {
    failIf(translate("some.translationkey"))
}

However, even if you provide a custom error response, you do not have access to the localized argument's name, so you're stuck with a translation key, no locale and no bundle to translate this for the user

Preferred Behaviour

Either having access to the ValidationContext in ArgumentParsingException or at least have access to the respective locale and bundle.

While it would be possible to simply pass everything as part of the error message:

validate {
    // Making it a single string would mean translating the command name twice and maintaining it in two spots.
    failIf("${translate("argument.name")}: ${translate("some.translationkey")}")
}

One way or another, this ends up adding more places to maintain, since you would have to hardcode the argument's translation key in the validate block, since you do not have access to the name in there.

Suggestions

Checking some of the places where ArgumentParsingException is thrown, passing the context or bundle and locale seems like it wouldn't be that hard. I'm guessing the context can be tricky, since these are shared with chat commands and I'm guessing those are very different (haven't worked with chat commands)

If this change is feasible, I can try it adding it myself.

Media

Default error response ![image](https://github.com/Kord-Extensions/kord-extensions/assets/12865379/d4a205f4-67f2-4d58-956a-31e4d2dc8e3d)
Attempt at custom error response ![image](https://github.com/Kord-Extensions/kord-extensions/assets/12865379/7758c989-1333-41cb-9653-7e62b10d493b)
gdude2002 commented 1 year ago

What's your use-case for this?

Galarzaa90 commented 1 year ago

What's your use-case for this?

If using errorResponse builder to define a custom response, there is no way to localize the response (besides the error message itself, that should already be localized from the validation context).

If you have localized argument names and descriptions, you will have access to the translation keys, but you cannot translate them because you have no access to the bundle to at least show it on the default language.

The default message is localized, but being able to customize it with information, like specifying which argument failed, and maybe providing the user with the argument's description as a reminder of what the argument is meant for can be helpful for the user to fix it.

Example

image

gdude2002 commented 1 year ago

I've pushed a commit along with version 1.6.0-SNAPSHOT, which should be released in about 15 minutes.

I've gone ahead and added the locale and bundle to the event. Adding the validation context isn't really all that feasible right now, although I'm not entirely sure that's necessary.