Improve +error.svelte handling

This commit is contained in:
2026-05-16 19:19:31 +01:00
parent b51845528f
commit 07579310b2
4 changed files with 43 additions and 27 deletions

View File

@@ -31,6 +31,7 @@ export function ThrowApiError(e: unknown): never {
// Map ApiError 'code' values // Map ApiError 'code' values
if (e instanceof ApiError) { if (e instanceof ApiError) {
console.error(JSON.stringify(e), e.code, "ERRCODE")
let status = 500; let status = 500;
if (e.code === 'AUTH') { if (e.code === 'AUTH') {
status = 401; status = 401;
@@ -53,6 +54,7 @@ export function ThrowApiError(e: unknown): never {
// Fallback for other error kind // Fallback for other error kind
const genericMessage = e instanceof Error ? e.message : 'An unexpected error occurred.'; const genericMessage = e instanceof Error ? e.message : 'An unexpected error occurred.';
console.log(e)
throw error(500, { throw error(500, {
message: genericMessage, message: genericMessage,
name: 'CRITICAL_ERROR', name: 'CRITICAL_ERROR',

View File

@@ -4,26 +4,35 @@
import stopErr from '$lib/assets/img/stop-error.svg'; import stopErr from '$lib/assets/img/stop-error.svg';
import noResult from '$lib/assets/img/no-data.svg'; import noResult from '$lib/assets/img/no-data.svg';
import Button from '$lib/components/ui/form-elements/Button.svelte'; import Button from '$lib/components/ui/form-elements/Button.svelte';
import { IconAntennaBarsOff } from '@tabler/icons-svelte-runes';
</script> </script>
<!-- Will need to check error type, using the upstream code combined with response code --> <!-- Will need to check error type, using the upstream code combined with response code -->
{#if page.error} <!-- {#if page.error}
{JSON.stringify(page.error)} {JSON.stringify(page.error)}
STATUS: {page.status} STATUS: {page.status}
{/if} {/if} -->
<div class="error-wrapper"> <div class="error-wrapper">
{#if page.status == 404} {#if page.error?.code}
<!-- Warning no data image --> {#if page.error.code === 'NOT_FOUND'}
<img class="err-img" src={noResult} alt="" role="presentation" width="200" height="200" /> <img class="err-img" src={noResult} alt="" role="presentation" width="200" height="200" />
{:else if page.status == 503} {:else if page.error.code === 'RATE_LIMIT'}
<!-- Change to a GSM-R X Sign?? -->
<span>OFFLINE!</span>
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" /> <img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{:else if page.status == 403} {:else if page.error.code === 'VALIDATION'}
<span>UNAUTH</span> <img class="err-img" src={noResult} alt="" role="presentation" width="150" height="210" />
{:else if page.error.code === 'AUTH'}
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" /> <img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{:else if page.error.code === 'SERVER'}
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{:else if page.error.code === 'NETWORK_DISCONNECTED'}
<div class="err-img gentle-flash">
<IconAntennaBarsOff size={120} />
</div>
{:else}
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{/if}
{:else} {:else}
<!-- STOP Error image --> <!-- STOP Error image -->
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" /> <img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
@@ -34,17 +43,16 @@
{page.error?.message ?? 'An unexpected derailment occurred.'} {page.error?.message ?? 'An unexpected derailment occurred.'}
</p> </p>
{#if page.error?.owlCode == 'NETWORK_DISCONNECTED'} {#if page.error?.code}
<p>THISISANETWORKERR</p>
<p>Operational data is unavaliable when offline</p>
{:else}
<div class="debug-info"> <div class="debug-info">
<code>Ref: {page.error?.owlCode}</code> <code>Ref: {page.error.code}</code>
</div> </div>
{/if} {/if}
{#if page.error?.owlCode === 'NETWORK_DISCONNECTED'} {#if page.error?.code === 'NETWORK_DISCONNECTED'}
<Button onclick={() => window.location.reload()} color={'accent'}>Retry</Button> <Button onclick={() => window.location.reload()} color={'accent'}>Retry</Button>
{:else if page.error?.code === 'NOT_FOUND'}
<Button onclick={() => history.back()} color={'accent'}>Go Back</Button>
{:else} {:else}
<Button href={'/'} color={'accent'}>Return to Home</Button> <Button href={'/'} color={'accent'}>Return to Home</Button>
{/if} {/if}
@@ -75,6 +83,19 @@
} }
} }
@keyframes gentle-flash {
100% {
opacity: 1;
}
50% {
opacity: 0.25;
}
}
.gentle-flash {
animation: gentle-flash ease-in-out 2s infinite;
}
.label { .label {
color: #ff4444; color: #ff4444;
letter-spacing: 0.2rem; letter-spacing: 0.2rem;

View File

@@ -12,7 +12,7 @@ export const load: PageLoad = async ({ url, fetch }) => {
if (!locId) { if (!locId) {
error(400, { error(400, {
message: 'Location not provided', message: 'Location not provided',
owlCode: 'NO_LOCATION_IN_PATH' code: 'VALIDATION'
}); });
} }
@@ -21,7 +21,7 @@ export const load: PageLoad = async ({ url, fetch }) => {
if (!BoardLocation) { if (!BoardLocation) {
error(404, { error(404, {
message: `Location (${locId.toUpperCase()}) not found`, message: `Location (${locId.toUpperCase()}) not found`,
owlCode: 'INVALID_LOCATION_CODE' code: 'VALIDATION'
}); });
} }
const title = BoardLocation.n || BoardLocation.t || 'Live Arr/Dep'; const title = BoardLocation.n || BoardLocation.t || 'Live Arr/Dep';
@@ -35,12 +35,6 @@ export const load: PageLoad = async ({ url, fetch }) => {
boardData boardData
}; };
} catch (e: unknown) { } catch (e: unknown) {
if (e instanceof TypeError && e.message === 'Failed to fetch') {
error(503, {
message: 'Cannot connect to the OwlBoard server',
owlCode: 'NETWORK_DISCONNECTED'
});
}
throw e; throw e;
} }
}; };

View File

@@ -1,7 +1,6 @@
import { OwlClient, ApiError, ValidationError, ThrowApiError } from '$lib/owlClient'; import { OwlClient, ApiError, ValidationError, ThrowApiError } from '$lib/owlClient';
import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts'; import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts';
import type { PageLoad } from './$types'; import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';
export const load: PageLoad = async ({ fetch, url }) => { export const load: PageLoad = async ({ fetch, url }) => {
const headcode = url.searchParams.get('h'); const headcode = url.searchParams.get('h');