2022-10-10 09:19:10 -04:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2022-10-10 09:19:10 -04:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-10-10 09:19:10 -04:00
|
|
|
*/
|
|
|
|
|
|
2023-11-30 23:47:26 -05:00
|
|
|
import type { DefaultNamespace, ParseKeys, TFunction, TOptions } from "i18next";
|
2023-11-17 12:26:25 +00:00
|
|
|
|
2022-10-10 09:19:10 -04:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2023-11-17 12:26:25 +00:00
|
|
|
public constructor(
|
|
|
|
|
messageKey: ParseKeys<DefaultNamespace, TOptions>,
|
|
|
|
|
translationFn: TFunction<DefaultNamespace>,
|
|
|
|
|
) {
|
2024-12-04 14:51:29 +00:00
|
|
|
super(translationFn(messageKey, { lng: "en" } as TOptions));
|
2022-10-10 09:19:10 -04:00
|
|
|
this.translatedMessage = translationFn(messageKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TranslatedErrorImpl extends TranslatedError {}
|
|
|
|
|
|
|
|
|
|
// i18next-parser can't detect calls to a constructor, so we expose a bare
|
|
|
|
|
// function instead
|
2023-09-22 18:05:13 -04:00
|
|
|
export const translatedError = (
|
2023-11-17 12:26:25 +00:00
|
|
|
messageKey: ParseKeys<DefaultNamespace, TOptions>,
|
2023-11-30 23:47:26 -05:00
|
|
|
t: TFunction<"app", undefined>,
|
2023-09-22 18:05:13 -04:00
|
|
|
): TranslatedError => new TranslatedErrorImpl(messageKey, t);
|