Files
LaDOSE/LaDOSE.Src/LaDOSE.WebApp/src/lib/api/errors.ts
T
2026-08-06 09:58:06 +02:00

22 lines
756 B
TypeScript

import { goto } from '$app/navigation';
import { session } from '$lib/stores/session.svelte';
import { ApiError } from './client';
/**
* Turns a failed call into something to show the user.
*
* A 401 means the 16-minute JWT lapsed mid-session: there is nothing useful to
* display, so the session is dropped and the user is sent back to `/login`, and
* this returns null.
*/
export function toErrorMessage(cause: unknown, fallback: string): string | null {
if (cause instanceof ApiError && cause.status === 401) {
session.clear();
void goto('/login', { replaceState: true });
return null;
}
// ApiError already carries the API's own `message`, or "unreachable" for status 0.
return cause instanceof ApiError ? cause.message : fallback;
}