'Genericise' errors ready for +error.svelte upgrade to handle known errors
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user