import { error, redirect } from '@sveltejs/kit'; import { env } from '$env/dynamic/private'; export async function load({ params, fetch }) { const { id } = params; const baseUrl = env.API_INTERNAL_URL || 'http://localhost:8080'; try { const res = await fetch(`${baseUrl}/api/documents/${id}`); if (res.status === 404) { throw error(404, 'Dokument nicht gefunden'); } if (res.status === 401) { throw redirect(302, '/login'); } if (!res.ok) { console.error(`Backend Fehler (${res.status}):`, res.statusText); throw error(500, 'Fehler beim Laden des Dokuments'); } const document = await res.json(); return { document }; } catch (e) { // Fehlerbehandlung if (e.status) throw e; // Redirects und HttpErrors durchlassen console.error("Ladefehler:", e); throw error(500, 'Verbindung zum Server fehlgeschlagen'); } }