'Genericise' errors ready for +error.svelte upgrade to handle known errors

This commit is contained in:
2026-05-16 18:19:47 +01:00
parent 808b576df1
commit b51845528f
7 changed files with 97 additions and 60 deletions

View File

@@ -9,8 +9,8 @@
<!-- Will need to check error type, using the upstream code combined with response code -->
{#if page.error}
{JSON.stringify(page.error)}
STATUS: {page.status}
{JSON.stringify(page.error)}
STATUS: {page.status}
{/if}
<div class="error-wrapper">

View File

@@ -15,7 +15,13 @@
import logoPlain from '$lib/assets/round-logo.svg';
import favicon from '$lib/assets/round-logo.svg';
import { IconHome, IconDialpad, IconSettings, IconHelp, IconDots } from '@tabler/icons-svelte-runes';
import {
IconHome,
IconDialpad,
IconSettings,
IconHelp,
IconDots
} from '@tabler/icons-svelte-runes';
onMount(async () => {
LOCATIONS.init();

View File

@@ -11,19 +11,15 @@
</script>
<div class="logo-container">
<button
type="button"
class="logo-btn"
onclick={handleLogoTap}
class:animate={isSpinning}
aria-label="Spin the OwlBoard logo"
>
<img
class="logo-img"
src={logo}
alt="OwlBoard Logo"
/>
</button>
<button
type="button"
class="logo-btn"
onclick={handleLogoTap}
class:animate={isSpinning}
aria-label="Spin the OwlBoard logo"
>
<img class="logo-img" src={logo} alt="OwlBoard Logo" />
</button>
</div>
<section class="about">
@@ -66,17 +62,15 @@
}
.logo-btn {
background: none;
border: none;
padding: 0;
cursor: pointer;
display: inline-block;
padding-top: 25px;
margin: auto;
width: clamp(80px, 20vw, 200px);
}
background: none;
border: none;
padding: 0;
cursor: pointer;
display: inline-block;
padding-top: 25px;
margin: auto;
width: clamp(80px, 20vw, 200px);
}
@keyframes owl-spin {
0% {
@@ -91,14 +85,14 @@
}
.logo-img {
width: 100%;
height: auto;
display: block;
}
width: 100%;
height: auto;
display: block;
}
.logo-btn.animate {
animation: owl-spin 0.8s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
animation: owl-spin 0.8s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
section {
margin: auto;

View File

@@ -25,7 +25,10 @@
console.log(e);
errorState = { status: 20, message: e.message };
} else {
errorState = { status: 0, message: `Unknown Error: ${e instanceof Error ? e.message : String(e)}` };
errorState = {
status: 0,
message: `Unknown Error: ${e instanceof Error ? e.message : String(e)}`
};
}
} finally {
resultsLoaded = true;
@@ -45,7 +48,10 @@
console.log(e);
errorState = { status: 20, message: e.message };
} else {
errorState = { status: 0, message: `Unknown Error: ${e instanceof Error ? e.message : String(e)}` };
errorState = {
status: 0,
message: `Unknown Error: ${e instanceof Error ? e.message : String(e)}`
};
}
} finally {
resultsLoaded = true;

View File

@@ -1,4 +1,4 @@
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
import { OwlClient, ApiError, ValidationError, ThrowApiError } from '$lib/owlClient';
import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts';
import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';
@@ -10,31 +10,21 @@ export const load: PageLoad = async ({ fetch, url }) => {
const date: string | Date = dateParam === '' || dateParam === null ? new Date() : dateParam;
if (!headcode) {
throw error(400, {
message: 'Headcode not provided',
owlCode: 'INVALID_DATA'
});
}
// Validation removed, handled by the API Client Lib
// Declared outside of the try so that it can be used in both the try and catch blocks
let results: ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse[];
try {
const response = await OwlClient.trains.getByHeadcode(headcode, date, toc, fetch);
const response = await OwlClient.trains.getByHeadcode(headcode || '', date, toc, fetch);
let title = !!headcode ? headcode.toUpperCase() : 'Error';
results = response.data;
return {
title: headcode.toUpperCase(),
title: title,
results: results
};
} 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;
ThrowApiError(e);
}
};