fix: improve error handling in paste fetching and add error page

This commit is contained in:
2026-01-20 17:30:54 -05:00
parent 8accfa2dc5
commit 5e4afafac0
3 changed files with 28 additions and 5 deletions
@@ -0,0 +1,24 @@
<script lang="ts">
import { page } from '$app/state';
$: status = page.status;
$: error = page.error;
$: message = error?.message || 'An unexpected error occurred.';
</script>
<svelte:head>
<title>{status} - Error - Sharpbin</title>
<meta name="description" content="An error occurred while processing your request." />
</svelte:head>
<main
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
>
<div
class="w-[95%] max-w-7xl rounded border-2
border-neutral-800 bg-neutral-900 p-6 text-center"
>
<h1 class="mb-4 text-4xl font-bold">Error {status}</h1>
<p class="mb-2 text-lg">{message}</p>
</div>
</main>
@@ -2,6 +2,7 @@ import type { PageServerLoad } from './$types';
import type { Paste } from '$lib/types/paste'; import type { Paste } from '$lib/types/paste';
import { getServerToken } from '$lib/utils/auth'; import { getServerToken } from '$lib/utils/auth';
import { env } from '$env/dynamic/private'; import { env } from '$env/dynamic/private';
import { error } from '@sveltejs/kit';
const apiUrl = process.env.VITE_API_URL || 'http://localhost:5050'; const apiUrl = process.env.VITE_API_URL || 'http://localhost:5050';
const apiKey = const apiKey =
@@ -22,10 +23,7 @@ export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent
}); });
if (pasteRes.status === 404) { if (pasteRes.status === 404) {
return { throw error(pasteRes.status, 'Paste not found.');
status: 404,
error: { message: 'Paste not found' }
};
} }
const viewed = await fetch(`${apiUrl}/api/paste/${id}/view`, { const viewed = await fetch(`${apiUrl}/api/paste/${id}/view`, {
@@ -1,13 +1,14 @@
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import type { Paste } from '$lib/types/paste'; import type { Paste } from '$lib/types/paste';
import { extractError } from '$lib/utils/misc'; import { extractError } from '$lib/utils/misc';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ fetch }) => { export const load: PageServerLoad = async ({ fetch }) => {
try { try {
const res = await fetch('/api/paste/recent'); const res = await fetch('/api/paste/recent');
if (!res.ok) { if (!res.ok) {
const errorMsg = extractError(await res.json()) || 'Failed to fetch recent pastes.'; const errorMsg = extractError(await res.json()) || 'Failed to fetch recent pastes.';
throw new Error(errorMsg); throw error(res.status, errorMsg);
} }
const recentPastes: Paste[] = await res.json(); const recentPastes: Paste[] = await res.json();
return { recentPastes }; return { recentPastes };