fix(errors): surface Bible verses on section error pages

SvelteKit's handleError hook is skipped for expected `error()` throws,
so verses set there never reached `$page.error` for server-thrown 404s
and auth denials. Introduce `errorWithVerse()` in `$lib/server/errorQuote`
that fetches a random verse first, then throws `error(status, body)`
with `{ message, bibleQuote, lang }`, making the quote available in
every `SectionError`. Convert all page load throws (catchalls, layout
validators, calendar, prayers, recipes, fitness, cospend, admin) and
hooks.server auth gates to the helper. Add `src/error.html` as a
branded last-resort fallback.
This commit is contained in:
2026-04-20 22:39:04 +02:00
parent fbd09fbdae
commit ad154bf914
33 changed files with 311 additions and 141 deletions
@@ -0,0 +1,5 @@
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = ({ fetch, url }) =>
errorWithVerse(fetch, url.pathname, 404, 'Not found');
@@ -1,5 +0,0 @@
import { error } from '@sveltejs/kit';
export const load = () => {
error(404, 'Not found');
};
@@ -1,9 +1,10 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, fetch }) => {
export const load: PageServerLoad = async ({ locals, fetch, url }) => {
const session = await locals.auth();
if (!session) {
throw redirect(302, '/login');
}
@@ -14,11 +15,11 @@ export const load: PageServerLoad = async ({ locals, fetch }) => {
fetch('/api/cospend/balance'),
fetch('/api/cospend/debts')
]);
if (!balanceResponse.ok) {
throw new Error('Failed to fetch balance');
}
if (!debtResponse.ok) {
throw new Error('Failed to fetch debt data');
}
@@ -33,6 +34,6 @@ export const load: PageServerLoad = async ({ locals, fetch }) => {
};
} catch (e) {
console.error('Error loading dashboard data:', e);
throw error(500, 'Failed to load dashboard data');
await errorWithVerse(fetch, url.pathname, 500, 'Failed to load dashboard data');
}
};
@@ -1,5 +1,6 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, fetch, url }) => {
const session = await locals.auth();
@@ -29,6 +30,6 @@ export const load: PageServerLoad = async ({ locals, fetch, url }) => {
};
} catch (e) {
console.error('Error loading payments data:', e);
throw error(500, 'Failed to load payments data');
await errorWithVerse(fetch, url.pathname, 500, 'Failed to load payments data');
}
};
@@ -1,7 +1,8 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, params, fetch }) => {
export const load: PageServerLoad = async ({ locals, params, fetch, url }) => {
const session = await locals.auth();
if (!session) {
@@ -23,6 +24,6 @@ export const load: PageServerLoad = async ({ locals, params, fetch }) => {
};
} catch (e) {
console.error('Error loading payment data:', e);
throw error(500, 'Failed to load payment data');
await errorWithVerse(fetch, url.pathname, 500, 'Failed to load payment data');
}
};
@@ -1,13 +1,18 @@
<script lang="ts">
import { page } from '$app/stores';
import SectionError from '$lib/components/SectionError.svelte';
import { page } from '$app/stores';
let faithLang = $derived($page.params.faithLang);
let isEnglish = $derived(faithLang === 'faith');
let sectionLabel = $derived(
faithLang === 'fides'
? { en: 'Fides', de: 'Fides' }
: { en: 'Faith', de: 'Glaube' }
);
</script>
<SectionError
sectionHref="/{faithLang}"
sectionLabel={{ en: 'Faith', de: 'Glaube' }}
{sectionLabel}
{isEnglish}
/>
@@ -1,10 +1,10 @@
import type { LayoutServerLoad } from "./$types"
import { error } from "@sveltejs/kit";
import { errorWithVerse } from "$lib/server/errorQuote"
export const load : LayoutServerLoad = async ({locals, params}) => {
export const load : LayoutServerLoad = async ({locals, params, fetch, url}) => {
// Validate faithLang parameter
if (params.faithLang !== 'glaube' && params.faithLang !== 'faith' && params.faithLang !== 'fides') {
throw error(404, 'Not found');
await errorWithVerse(fetch, url.pathname, 404, 'Not found');
}
const lang = params.faithLang === 'faith' ? 'en' : params.faithLang === 'fides' ? 'la' : 'de';
@@ -0,0 +1,5 @@
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = ({ fetch, url }) =>
errorWithVerse(fetch, url.pathname, 404, 'Not found');
@@ -1,5 +0,0 @@
import { error } from '@sveltejs/kit';
export const load = () => {
error(404, 'Not found');
};
@@ -1,10 +1,11 @@
import { error, redirect } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { expectedSlug } from './calendarI18n';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ params, url }) => {
export const load: PageServerLoad = async ({ params, url, fetch }) => {
const slug = expectedSlug(params.faithLang);
if (slug === null) throw error(404, 'Not found');
if (slug === null) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
if (params.calendar !== slug) {
throw redirect(307, `/${params.faithLang}/${slug}`);
}
@@ -1,5 +1,6 @@
import { error, redirect } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
import {
DEFAULT_DIOCESE_1962,
DEFAULT_DIOCESE_1969,
@@ -29,9 +30,9 @@ export type {
YearDay
} from '$lib/calendarTypes';
export const load: PageServerLoad = async ({ params, url, locals }) => {
export const load: PageServerLoad = async ({ params, url, locals, fetch }) => {
const slug = expectedSlug(params.faithLang);
if (slug === null) throw error(404, 'Not found');
if (slug === null) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
if (params.calendar !== slug) {
throw redirect(307, `/${params.faithLang}/${slug}`);
}
@@ -52,7 +53,7 @@ export const load: PageServerLoad = async ({ params, url, locals }) => {
// Reject mm without yyyy, dd without yyyy+mm. Sveltekit optional routes let
// gaps through so we normalize here.
if ((params.mm && !params.yyyy) || (params.dd && !params.mm)) {
throw error(404, 'Not found');
await errorWithVerse(fetch, url.pathname, 404, 'Not found');
}
const today = new Date();
@@ -92,7 +93,7 @@ export const load: PageServerLoad = async ({ params, url, locals }) => {
const daysInMonth = new Date(year, month + 1, 0).getDate();
if (params.dd) {
const ddNum = Number(params.dd);
if (ddNum < 1 || ddNum > daysInMonth) throw error(404, 'Not found');
if (ddNum < 1 || ddNum > daysInMonth) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
}
// Tentative selectedIso used only for the LY rollover decision. The real
// selectedIso is recomputed after monthDays below (same logic, now on the
@@ -1,5 +1,6 @@
import { error, redirect } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
import {
DEFAULT_DIOCESE_1962,
DEFAULT_DIOCESE_1969,
@@ -13,9 +14,9 @@ import {
} from '../../../../../calendarI18n';
import { getYear, getYear1962, isoFor } from '$lib/server/liturgicalCalendar';
export const load: PageServerLoad = async ({ params, url, locals }) => {
export const load: PageServerLoad = async ({ params, url, locals, fetch }) => {
const slug = expectedSlug(params.faithLang);
if (slug === null) throw error(404, 'Not found');
if (slug === null) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
if (params.calendar !== slug) {
throw redirect(307, `/${params.faithLang}/${slug}`);
}
@@ -38,10 +39,10 @@ export const load: PageServerLoad = async ({ params, url, locals }) => {
const month = Number(params.mm) - 1;
const day = Number(params.dd);
if (!Number.isFinite(year) || year < minYear || year > 2100) throw error(404, 'Not found');
if (!Number.isFinite(month) || month < 0 || month > 11) throw error(404, 'Not found');
if (!Number.isFinite(year) || year < minYear || year > 2100) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
if (!Number.isFinite(month) || month < 0 || month > 11) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
const daysInMonth = new Date(year, month + 1, 0).getDate();
if (!Number.isFinite(day) || day < 1 || day > daysInMonth) throw error(404, 'Not found');
if (!Number.isFinite(day) || day < 1 || day > daysInMonth) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
const iso = isoFor(year, month, day);
const yearMap =
@@ -49,7 +50,7 @@ export const load: PageServerLoad = async ({ params, url, locals }) => {
? await getYear1962(lang, diocese1962, year)
: await getYear(lang, diocese1969, year);
const entry = yearMap.get(iso);
if (!entry) throw error(404, 'Not found');
if (!entry) await errorWithVerse(fetch, url.pathname, 404, 'Not found');
const today = new Date();
const todayIso = today.toISOString().slice(0, 10);
@@ -1,12 +1,13 @@
import type { PageServerLoad, Actions } from "./$types";
import { error } from "@sveltejs/kit";
import { errorWithVerse } from '$lib/server/errorQuote';
import { validPrayerSlugs } from '$lib/data/prayerSlugs';
const angelusSlugs = new Set(['angelus', 'regina-caeli']);
export const load: PageServerLoad = async ({ params, url, locals, fetch }) => {
if (!validPrayerSlugs.has(params.prayer)) {
throw error(404, 'Prayer not found');
await errorWithVerse(fetch, url.pathname, 404, 'Prayer not found');
}
const latinParam = url.searchParams.get('latin');
@@ -1,6 +1,6 @@
<script lang="ts">
import { page } from '$app/stores';
import SectionError from '$lib/components/SectionError.svelte';
import { page } from '$app/stores';
let recipeLang = $derived($page.params.recipeLang);
let isEnglish = $derived(recipeLang === 'recipes');
@@ -1,10 +1,10 @@
import type { LayoutServerLoad } from "./$types"
import { error } from "@sveltejs/kit";
import { errorWithVerse } from "$lib/server/errorQuote"
export const load : LayoutServerLoad = async ({locals, params}) => {
export const load : LayoutServerLoad = async ({locals, params, fetch, url}) => {
// Validate recipeLang parameter
if (params.recipeLang !== 'rezepte' && params.recipeLang !== 'recipes') {
throw error(404, 'Not found');
await errorWithVerse(fetch, url.pathname, 404, 'Not found');
}
const lang = params.recipeLang === 'recipes' ? 'en' : 'de';
@@ -0,0 +1,5 @@
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = ({ fetch, url }) =>
errorWithVerse(fetch, url.pathname, 404, 'Not found');
@@ -1,5 +0,0 @@
import { error } from '@sveltejs/kit';
export const load = () => {
error(404, 'Not found');
};
@@ -1,15 +1,16 @@
import { redirect, error } from '@sveltejs/kit';
import type { PageServerLoad, Actions } from './$types';
import { stripHtmlTags } from '$lib/js/stripHtmlTags';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ fetch, params, locals }) => {
export const load: PageServerLoad = async ({ fetch, params, locals, url }) => {
const isEnglish = params.recipeLang === 'recipes';
const apiBase = `/api/${params.recipeLang}`;
const res = await fetch(`${apiBase}/items/${params.name}`);
if (!res.ok) {
throw error(res.status, 'Recipe not found');
await errorWithVerse(fetch, url.pathname, res.status, 'Recipe not found');
}
const item = await res.json();
@@ -1,7 +1,8 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, url }) => {
export const load: PageServerLoad = async ({ locals, url, fetch }) => {
const session = await locals.auth();
// Redirect to login if not authenticated
@@ -12,7 +13,7 @@ export const load: PageServerLoad = async ({ locals, url }) => {
// Check user group permission
if (!session.user.groups?.includes('rezepte_users')) {
throw error(403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
await errorWithVerse(fetch, url.pathname, 403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
}
return {
@@ -1,7 +1,8 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, url }) => {
export const load: PageServerLoad = async ({ locals, url, fetch }) => {
const session = await locals.auth();
if (!session?.user?.nickname) {
@@ -10,7 +11,7 @@ export const load: PageServerLoad = async ({ locals, url }) => {
}
if (!session.user.groups?.includes('rezepte_users')) {
throw error(403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
await errorWithVerse(fetch, url.pathname, 403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
}
return {
@@ -1,5 +1,6 @@
import type { PageServerLoad } from "./$types";
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ fetch, locals, url, params }) => {
const session = await locals.auth();
@@ -12,7 +13,7 @@ export const load: PageServerLoad = async ({ fetch, locals, url, params }) => {
// Check user group permission
if (!session.user.groups?.includes('rezepte_users')) {
throw error(403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
await errorWithVerse(fetch, url.pathname, 403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
}
try {
@@ -1,7 +1,8 @@
import type { PageServerLoad } from './$types';
import { redirect, error } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ locals, params, url }) => {
export const load: PageServerLoad = async ({ locals, params, url, fetch }) => {
const session = await locals.auth();
// Redirect to login if not authenticated
@@ -12,7 +13,7 @@ export const load: PageServerLoad = async ({ locals, params, url }) => {
// Check user group permission
if (!session.user.groups?.includes('rezepte_users')) {
throw error(403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
await errorWithVerse(fetch, url.pathname, 403, 'Zugriff verweigert. Du hast keine Berechtigung für diesen Bereich.');
}
return {
@@ -0,0 +1,5 @@
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = ({ fetch, url }) =>
errorWithVerse(fetch, url.pathname, 404, 'Not found');
-5
View File
@@ -1,5 +0,0 @@
import { error } from '@sveltejs/kit';
export const load = () => {
error(404, 'Not found');
};
@@ -1,5 +1,5 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ params, fetch, url }) => {
const lang = url.pathname.includes('/uebungen') ? 'de' : 'en';
@@ -10,7 +10,7 @@ export const load: PageServerLoad = async ({ params, fetch, url }) => {
]);
if (!exerciseRes.ok) {
error(404, 'Exercise not found');
await errorWithVerse(fetch, url.pathname, 404, 'Exercise not found');
}
const exerciseData = await exerciseRes.json();
@@ -1,11 +1,11 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = async ({ params, fetch }) => {
export const load: PageServerLoad = async ({ params, fetch, url }) => {
const res = await fetch(`/api/fitness/sessions/${params.id}`);
if (!res.ok) {
error(404, 'Session not found');
await errorWithVerse(fetch, url.pathname, 404, 'Session not found');
}
return {
@@ -1,5 +1,5 @@
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
import { errorWithVerse } from '$lib/server/errorQuote';
import { NUTRITION_DB } from '$lib/data/nutritionDb';
import { BLS_DB } from '$lib/data/blsDb';
import { DRI_MALE } from '$lib/data/dailyReferenceIntake';
@@ -60,16 +60,16 @@ async function computeRecipePer100g(id: string): Promise<Record<string, number>>
return per100g;
}
export const load: PageServerLoad = async ({ params, url }) => {
export const load: PageServerLoad = async ({ params, url, fetch }) => {
const { source, id } = params;
if (source !== 'bls' && source !== 'usda' && source !== 'recipe' && source !== 'off' && source !== 'custom') {
throw error(404, 'Invalid source');
await errorWithVerse(fetch, url.pathname, 404, 'Invalid source');
}
if (source === 'bls') {
const entry = BLS_DB.find(e => e.blsCode === id);
if (!entry) throw error(404, 'Food not found');
if (!entry) await errorWithVerse(fetch, url.pathname, 404, 'Food not found');
return {
food: {
source: 'bls' as const,
@@ -91,7 +91,7 @@ export const load: PageServerLoad = async ({ params, url }) => {
const recipe = await Recipe.findOne(recipeQuery)
.select('short_name name translations images')
.lean();
if (!recipe) throw error(404, 'Recipe not found');
if (!recipe) await errorWithVerse(fetch, url.pathname, 404, 'Recipe not found');
// Use logged per100g from food diary entry if provided, otherwise compute from current recipe
const logEntryId = url.searchParams.get('logEntry');
@@ -131,7 +131,7 @@ export const load: PageServerLoad = async ({ params, url }) => {
if (source === 'off') {
await dbConnect();
const entry = await OpenFoodFact.findOne({ barcode: id }).lean();
if (!entry) throw error(404, 'Food not found');
if (!entry) await errorWithVerse(fetch, url.pathname, 404, 'Food not found');
const portions: { description: string; grams: number }[] = [];
if (entry.serving?.grams) {
portions.push(entry.serving as { description: string; grams: number });
@@ -156,7 +156,7 @@ export const load: PageServerLoad = async ({ params, url }) => {
if (source === 'custom') {
await dbConnect();
const meal = await CustomMeal.findById(id).lean();
if (!meal) throw error(404, 'Meal not found');
if (!meal) await errorWithVerse(fetch, url.pathname, 404, 'Meal not found');
// Aggregate per100g from ingredients
const totals: Record<string, number> = {};
@@ -211,7 +211,7 @@ export const load: PageServerLoad = async ({ params, url }) => {
// USDA
const fdcId = Number(id);
const entry = NUTRITION_DB.find(e => e.fdcId === fdcId);
if (!entry) throw error(404, 'Food not found');
if (!entry) await errorWithVerse(fetch, url.pathname, 404, 'Food not found');
return {
food: {
source: 'usda' as const,
@@ -0,0 +1,5 @@
import type { PageServerLoad } from './$types';
import { errorWithVerse } from '$lib/server/errorQuote';
export const load: PageServerLoad = ({ fetch, url }) =>
errorWithVerse(fetch, url.pathname, 404, 'Not found');
-5
View File
@@ -1,5 +0,0 @@
import { error } from '@sveltejs/kit';
export const load = () => {
error(404, 'Not found');
};