Files
TREK/client/src/i18n/translateApiError.ts
T
jubnl 819aa793ae v3.1.4 (#1371)
* fix(backups): prevent recursion in path that is backed up

* fix(share): show user currency instead of the default euro in the share page

* fix(files): show descriptive error for unsupported upload type

Unsupported file uploads showed a generic 'Upload failed' toast even
though the server already returns a descriptive 400. The client catch
blocks discarded the error and always showed t('files.uploadError').

The server now emits the i18n key 'files.uploadErrorType' as its error
message; a new translateApiError() helper resolves a server message that
is a known translation key via t() and falls back to the generic key
otherwise. Wired into the three trip-file upload catch sites.

Closes #1363
2026-07-01 18:57:16 +02:00

18 lines
636 B
TypeScript

/**
* Resolve a server error whose message may be an i18n key.
*
* The server can return a translation key as its error message (e.g.
* `files.uploadErrorType`). `t()` returns the key unchanged when it isn't a
* known translation, so `translated === key` reliably means "not a key" — in
* that case we fall back to a generic, always-localized message.
*/
export function translateApiError(
t: (key: string) => string,
err: unknown,
fallbackKey: string,
): string {
const key = err instanceof Error ? err.message : ''
const translated = t(key)
return translated && translated !== key ? translated : t(fallbackKey)
}