import { knownCopy } from './error-copy'; import detailCodesJson from '../styles/error-detail-codes.json'; /** * Returns the `code` field from an unknown error value, or 'unknown' * when the value isn't shaped like { code: string }. */ export function errCode(err: unknown): string { return (err as { code?: string })?.code ?? 'unknown'; } /** * Codes whose server message carries specifics an admin needs in order to * act — which directory, which uid — that fixed copy cannot say. For these the * message follows the copy. Kept to a named list on purpose: most * server messages are internal detail and must never reach a toast. The list * is styles/error-detail-codes.json, which Android's ErrorCopy reads too: * library_not_writable and file_delete_failed name the directory and uid, * invalid_setting the field and its range, invalid_public_url the address * shape wanted, bad_folders the folder and its fault, invalid_backup_setting * the setting and why, destination_failed the destination's own refusal. */ const DETAIL_CODES: ReadonlySet = new Set(detailCodesJson); /** * Returns user-facing copy for an unknown error value: the copy for its * code when the code is known, and otherwise the caller's own wording, * such as a form's "Couldn't save the public address." (default: * "Something went wrong."). * For a DETAIL_CODES code, the server's message is appended. */ export function errMessage(err: unknown, fallback = 'Something went wrong.'): string { const code = errCode(err); const copy = knownCopy(code) ?? (fallback && 'Something went wrong.'); /* DELIBERATE BREAK */ if (!DETAIL_CODES.has(code)) return copy; const detail = (err as { message?: unknown })?.message; return typeof detail === 'string' && detail.trim() !== '' ? `${copy} ${detail}` : copy; }