From 551ff06adaa568e34d569593524a3886553b65a9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Yusuf=20Akg=C3=BCl?= <s86116@bht-berlin.de>
Date: Wed, 24 May 2023 18:22:22 +0200
Subject: [PATCH] fix deployment url 4 error catching

---
 lib/igdb.ts | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/lib/igdb.ts b/lib/igdb.ts
index b924e9b..31ba2b3 100644
--- a/lib/igdb.ts
+++ b/lib/igdb.ts
@@ -28,13 +28,13 @@ async function getToken(): Promise<IAuth> {
 }
 
 // fetches the top 200 games with a rating of 96 or higher
-export async function getGames(page = 1): Promise<IGame[] | unknown> {
-    const auth = await getToken()
-    const url = new URL(`${IGDB_BASE_URL}/games`)
+export async function getGames(page = 1): Promise<IGame[]> {
+    try {
+        const auth = await getToken();
+        const url = new URL(`${IGDB_BASE_URL}/games`);
 
-    let offset = calculateOffset(page, limit)
+        let offset = calculateOffset(page, limit);
 
-    try {
         const response = await fetch(url, {
             method: 'POST',
             headers: {
@@ -42,18 +42,24 @@ export async function getGames(page = 1): Promise<IGame[] | unknown> {
                 'Authorization': `Bearer ${auth.access_token}`
             },
             body: `fields name, cover.*; limit ${limit}; offset ${offset};
-            sort total_rating desc; where total_rating_count > 2
-            & cover != null & total_rating != null & rating != null;`
-        })
-        const games = await response.json() as IGame[]
+                sort total_rating desc; where total_rating_count > 2
+                & cover != null & total_rating != null & rating != null;`
+        });
+
+        if (!response.ok) {
+            throw new Error(`Error fetching games: ${response.statusText}`);
+        }
+
+        const games = await response.json() as IGame[];
 
         games.forEach(game => {
-            game.cover.url = getImageURL(game.cover.image_id, 'cover_big')
-        })
+            game.cover.url = getImageURL(game.cover.image_id, 'cover_big');
+        });
 
-        return games
+        return games;
     } catch (error) {
-        console.log(error)
+        console.error('Error in getGames:', error);
+        throw error;
     }
 }
 
-- 
GitLab