Open History to see the last commit
1import { isMsg, mFallback, type M, type Msg } from "gt-i18n";
2import * as Schema from "effect/Schema";
3
4/** What `msg()` returns: plain text, or an object that carries the text's values or context. */
5export type Marked = string | Msg;
6export const Marked = Schema.Union([Schema.String, Schema.declare(isMsg)]);
7export const isMarked = Schema.is(Marked);
8
9/**
10 * One or more `msg()` sentences shown together, joined by a space. Errors carry it beside
11 * their English `message` so logs and English comparisons keep working.
12 */
13export type MarkedText = ReadonlyArray<Marked>;
14export const MarkedText = Schema.Array(Marked);
15const isMarkedText = Schema.is(MarkedText);
16
17export function markedTextEnglish(text: MarkedText): string {
18 return resolveMarkedText(text);
19}
20
21export function resolveMarkedText(text: MarkedText, resolve: M = mFallback): string {
22 return text.map((sentence) => resolve(sentence)).join(" ");
23}
24
25/** `detail` in English plus the marked text, for errors whose `message` reads `detail`. */
26export function translatableDetail(...text: MarkedText): {
27 readonly detail: string;
28 readonly i18nMessage: MarkedText;
29} {
30 return { detail: markedTextEnglish(text), i18nMessage: text };
31}
32
33/** `message` in English plus the marked text. */
34export function translatableMessage(...text: MarkedText): {
35 readonly message: string;
36 readonly i18nMessage: MarkedText;
37} {
38 return { message: markedTextEnglish(text), i18nMessage: text };
39}
40
41export function errorI18nMessage(error: unknown): MarkedText | undefined {
42 if (typeof error !== "object" || error === null || !("i18nMessage" in error)) return undefined;
43 const text = error.i18nMessage;
44 return isMarkedText(text) ? text : undefined;
45}
46
47/**
48 * What to show for an error: its marked text in the active language when it has one,
49 * otherwise its non-empty `message`, otherwise null.
50 */
51export function errorDisplayMessage(error: unknown, resolve: M = mFallback): string | null {
52 const text = errorI18nMessage(error);
53 if (text !== undefined) return resolveMarkedText(text, resolve);
54 if (typeof error !== "object" || error === null || !("message" in error)) return null;
55 return typeof error.message === "string" && error.message.trim().length > 0
56 ? error.message
57 : null;
58}
59
60/**
61 * A thrown `Error`'s text in the active language, its plain `message` (even empty) when it has no
62 * marked text, and null for anything that is not an `Error`.
63 */
64export function thrownErrorMessage(error: unknown, resolve: M = mFallback): string | null {
65 if (!(error instanceof Error)) return null;
66 const text = errorI18nMessage(error);
67 return text === undefined ? error.message : resolveMarkedText(text, resolve);
68}
69
70/** Carries another error's message, and its marked text when it has one, into `detail`. */
71export function detailFromError(error: { readonly message: string }): {
72 readonly detail: string;
73 readonly i18nMessage?: MarkedText;
74} {
75 const text = errorI18nMessage(error);
76 return text === undefined
77 ? { detail: error.message }
78 : { detail: error.message, i18nMessage: text };
79}
80
81/** Carries another error's message, and its marked text when it has one, into `message`. */
82export function messageFromError(error: { readonly message: string }): {
83 readonly message: string;
84 readonly i18nMessage?: MarkedText;
85} {
86 const text = errorI18nMessage(error);
87 return text === undefined
88 ? { message: error.message }
89 : { message: error.message, i18nMessage: text };
90}
91
92/** A plain `Error` whose `message` stays English and that carries its marked text. */
93export function translatableError(
94 ...text: MarkedText
95): Error & { readonly i18nMessage: MarkedText } {
96 return Object.assign(new Error(markedTextEnglish(text)), { i18nMessage: text });
97}
98
99/**
100 * Attaches marked text to an error whose class lives in `@t3tools/contracts`, so it cannot
101 * declare the field. Use only when the error's `message` is exactly that text in English.
102 */
103export function withI18nMessage<E extends Error>(
104 error: E,
105 ...text: MarkedText
106): E & { readonly i18nMessage: MarkedText } {
107 return Object.assign(error, { i18nMessage: text });
108}