translateError
function in meocord/common Since 4.1.0
translateError(
error: unknown,
t: Translator<any>,
target: Interaction | Message | Locale | `${Locale}`,
): stringThe text MeoCord's fallback answers an error with, in a user's or a server's language.
Use it in an exception filter that answers MeoCord's errors its own way but keeps their words: a usage error's
usage and issues, a cooldown's wait, the cooldown store's refusal, "Command not found!". A guard's, a validation's
or a UserError's message is the app's and is returned as it is; any other error is the fallback's generic fault.
Parameters
| Name | Type | Description |
|---|---|---|
error | unknown | What the call threw. |
t | Translator<any> | The app's translator, made by |
target | Interaction | Message | Locale | `${Locale}` | The interaction or message being answered, or a locale. |
Returns
stringThe text, in the target's language where the catalogs have it.
Examples
@Catch(CooldownError)
export class CooldownFilter implements ExceptionFilter<CooldownError> {
constructor(private readonly t: Translator) {}
async catch(error: CooldownError, context: ExecutionContext) {
const interaction = context.getInteraction()
if (interaction?.isRepliable()) {
await interaction.reply({ content: `⏳ ${translateError(error, this.t, interaction)}`, flags: MessageFlags.Ephemeral })
}
}
}