import { ApiError } from "@/lib/api"; /** * What to tell an editor when a call fails. German, one sentence, no status * codes up front - the code is appended for anything unexpected, so a * screenshot of the message is still useful to whoever debugs it. * * 401 is listed for completeness; callers should send the user to the login * page instead of showing it (see `isSessionExpired`). */ export const describeApiError = (error: unknown): string => { if (!(error instanceof ApiError)) { return "Unerwarteter Fehler. Bitte versuche es erneut."; } switch (error.status) { case 0: return "Keine Verbindung zum Server. Bitte prüfe deine Internetverbindung."; case 401: return "Deine Sitzung ist abgelaufen."; case 403: return "Dein Konto hat keine Berechtigung (mehr) für den Kalender."; case 200: // A 200 that the client decided was a failure (e.g. "0 rows were updated") carries its own German message. return error.message; default: return `Der Server hat einen Fehler gemeldet (${error.status}). Bitte versuche es erneut.`; } }; /** Nobody is signed in any more - the one case worth a trip to the login page. */ export const isSessionExpired = (error: unknown): boolean => error instanceof ApiError && error.status === 401;