/* Copyright 2022-2024 New Vector Ltd. SPDX-License-Identifier: AGPL-3.0-only Please see LICENSE in the repository root for full details. */ import type { DefaultNamespace, ParseKeys, TFunction, TOptions } from "i18next"; /** * An error with messages in both English and the user's preferred language. */ // Abstract to force consumers to use the function below rather than calling the // constructor directly export abstract class TranslatedError extends Error { /** * The error message in the user's preferred language. */ public readonly translatedMessage: string; public constructor( messageKey: ParseKeys, translationFn: TFunction, ) { super(translationFn(messageKey, { lng: "en-GB" } as TOptions)); this.translatedMessage = translationFn(messageKey); } } class TranslatedErrorImpl extends TranslatedError {} // i18next-parser can't detect calls to a constructor, so we expose a bare // function instead export const translatedError = ( messageKey: ParseKeys, t: TFunction<"app", undefined>, ): TranslatedError => new TranslatedErrorImpl(messageKey, t);