Improve apiGet error handling

This commit is contained in:
Fred Boniface 2024-04-30 11:09:42 +01:00
parent 8bd97c308c
commit 1484a9068e
1 changed files with 5 additions and 7 deletions

View File

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