From 1484a9068e7b648ce6420e1b3bb89d5a15f8f655 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 30 Apr 2024 11:09:42 +0100 Subject: [PATCH] Improve apiGet error handling --- src/lib/scripts/apiFetch.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib/scripts/apiFetch.ts b/src/lib/scripts/apiFetch.ts index 22a1e4e..32f8198 100644 --- a/src/lib/scripts/apiFetch.ts +++ b/src/lib/scripts/apiFetch.ts @@ -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 { @@ -33,7 +33,7 @@ export async function apiGet(path: string): Promise { 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 { throw new Error('Invalid response. Require JSON.'); } return await res.json(); - } catch (err) { console.error('Error fetching data:', err); throw err; - } finally { unsubscribe(); }