Skip to content
GitHub

translateError

function in meocord/common Since 4.1.0

translateError(
  error: unknown,
  t: Translator<any>,
  target: Interaction | Message | Locale | `${Locale}`,
): string

The 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

NameTypeDescription
errorunknown

What the call threw.

tTranslator<any>

The app's translator, made by createTranslator.

targetInteraction | Message | Locale | `${Locale}`

The interaction or message being answered, or a locale.

Returns

string

The text, in the target's language where the catalogs have it.

Examples

TypeScript
@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 })
    }
  }
}