Kullanım
Hata sınıfı tanımlama, mesaj kaydetme, dil değiştirme, önek ve geri dönüş temel işlemleri.
Temel akış
Hata sınıfını tanımlayın
I18nErrorBase<TMeta> sınıfından türetin ve mesaj bilgisini TMeta olarak tanımlayın.
Dile özgü mesajları kaydedin
setErrorMessage ile her sınıf ve dilin mesajını kaydedin.
Dili değiştirip message değerini okuyun
setGlobalConfig({ lang }) ile değiştirin ve error.message okuyun.
import { setGlobalConfig } from "valibot";
import { I18nErrorBase, setErrorMessage } from "i18n-error-base";
class BusinessRuleError extends I18nErrorBase<{ code: string; limit: number }> {
public constructor(meta: { code: string; limit: number }) {
super(meta, ({ code, limit }) => `Code: ${code}, Limit: ${limit}`);
}
}
setErrorMessage(
BusinessRuleError,
({ code, limit }) => `コード: ${code}、制限: ${limit}`,
"ja",
);
const error = new BusinessRuleError({ code: "ERR", limit: 50 });
setGlobalConfig({ lang: "ja" });
console.log(error.message); // コード: ERR、制限: 50
setGlobalConfig({ lang: "en" });
console.log(error.message); // Code: ERR, Limit: 50
Hata sınıfını tanımlama
TMeta, mesaj üretimi için ek bilgidir. error.meta olarak saklanır ve kayıtlı işlevlere aktarılır.
import { I18nErrorBase } from "i18n-error-base";
class NotFoundError extends I18nErrorBase<{ resource: string }> {}
const error = new NotFoundError({ resource: "user" }, "fallback message");
console.log(error.meta); // { resource: "user" }
- bağımsız değişken dize veya
(meta) => stringişlevidir. 3. ise{ cause }ve diğerErrorseçenekleridir.
class TestError extends I18nErrorBase<{ code: string }> {}
const cause = new Error("original");
const error = new TestError({ code: "ERR" }, "something went wrong", { cause });
console.log(error.cause === cause); // true
TMeta değeri undefined olduğunda meta atlanabilir.
class NoMetaError extends I18nErrorBase<undefined> {}
const a = new NoMetaError("no meta");
const b = new NoMetaError(undefined, "no meta");
console.log(a.meta); // undefined
Mesajı kaydetme
setErrorMessage, sınıf ve dil bazında üretim işlevlerini kaydeder.
import { I18nErrorBase, setErrorMessage } from "i18n-error-base";
class TestError extends I18nErrorBase<{ code: string }> {}
setErrorMessage(TestError, ({ code }) => `エラーコード: ${code}`, "ja");
setErrorMessage(TestError, "something went wrong", "en");
Dize de verilebilir. Diller dizi olarak belirtilebilir.
const message = ({ code }: { code: string }) => `エラーコード: ${code}`;
setErrorMessage(TestError, message, ["ja", "ko", "zh"]);
Aynı sınıf ve dile yeniden kayıt üzerine yazar. Yalnızca kayıt ve başvuru oluşturucuları aynıysa kullanılır, instanceof ile değil.
Dili değiştirme
Valibot genel ayarıyla değiştirin. message her okumada güncel lang değerine başvurur.
import { setGlobalConfig } from "valibot";
setGlobalConfig({ lang: "ja" });
console.log(error.message); // 日本語の登録メッセージ
setGlobalConfig({ lang: "en" });
console.log(error.message); // 英語の登録メッセージ or 既定メッセージ
Ayarlanmamış lang "en" olur. Kayıtsız diller oluşturucunun varsayılan mesajını kullanır.
setErrorMessage(BusinessRuleError, () => "日本語メッセージ", "ja");
setGlobalConfig({ lang: "fr" });
console.log(error.message); // "Code: ERR, Limit: 50"(既定メッセージ)
Önek ekleme
static prefix, çözümlenmiş mesajın başına eklenir. Çevrilmiş ve varsayılan mesajlara uygulanır.
class PrefixedError extends I18nErrorBase<{ code: string }> {
static {
this.prefix = "[ERROR] ";
}
}
setErrorMessage(PrefixedError, ({ code }) => `code is ${code}`, "ja");
const error = new PrefixedError({ code: "ERR" }, "fallback");
setGlobalConfig({ lang: "ja" });
console.log(error.message); // "[ERROR] code is ERR"
setGlobalConfig({ lang: "en" });
console.log(error.message); // "[ERROR] fallback"
Ayarlanmazsa aynen döndürülür.
Error olarak davranış
error instanceof Error,instanceof I18nErrorBase,instanceofsomut sınıf tümütrueolurString(error)/error.toString()çözümlenmiş mesajı içerir (ör:"Error: ...")cause,{ cause }ile saklanır
Yaygın kalıplar
Varsayılan mesajı oluşturucuda sabitleme
class BusinessRuleError extends I18nErrorBase<{ code: string; limit: number }> {
public constructor(meta: { code: string; limit: number }, options?: ErrorOptions) {
super(meta, ({ code, limit }) => `Code: ${code}, Limit: ${limit}`, options);
}
}
Çağıran taraf yalnızca meta geçirir, varsayılan metin tekrarını önler.
Birden çok dili topluca kaydetme
setErrorMessage(MyError, "エラーが発生しました", ["ja"]);
setErrorMessage(MyError, "An error occurred", ["en", "en-US"]);
Dil kodları normalize edilmez. "en" ve "en-US" ayrı anahtarlardır, gerekli kodları açıkça kaydedin.