Improve apiGet error handling

This commit is contained in:
Fred Boniface 2024-04-30 11:09:42 +01:00
parent 8bd97c308c
commit 1484a9068e

View File

@ -2,15 +2,15 @@ import { dev } from '$app/environment';
import { uuid } from '$lib/stores/uuid'; import { uuid } from '$lib/stores/uuid';
import type { Unsubscriber } from 'svelte/store'; import type { Unsubscriber } from 'svelte/store';
const testUrl: string = 'http://localhost:8460';
const prodUrl: string = 'https://owlboard.info';
function getUrlString(): string { function getUrlString(): string {
if (dev) { if (dev) {
const testUrl: string = 'http://localhost:8460';
console.info('DEVMODE active, using testing URL: ', testUrl); console.info('DEVMODE active, using testing URL: ', testUrl);
return testUrl; return testUrl;
} else {
const currentUrl: string = `https://${window.location.host}`;
return currentUrl;
} }
return prodUrl;
} }
export async function apiGet(path: string): Promise<any> { export async function apiGet(path: string): Promise<any> {
@ -42,11 +42,9 @@ export async function apiGet(path: string): Promise<any> {
throw new Error('Invalid response. Require JSON.'); throw new Error('Invalid response. Require JSON.');
} }
return await res.json(); return await res.json();
} catch (err) { } catch (err) {
console.error('Error fetching data:', err); console.error('Error fetching data:', err);
throw err; throw err;
} finally { } finally {
unsubscribe(); unsubscribe();
} }