From 359de6e9f8d7ab1ae5db1f6a56d5f59f594cf220 Mon Sep 17 00:00:00 2001 From: catjoha04-byte Date: Tue, 10 Nov 2026 19:33:32 +0100 Subject: [PATCH 1/8] feat: add error message when server can't be reached --- Frontend/src/FrontendLogic/LogicConnection.ts | 24 ++++++++++++------- Frontend/src/tests/WishlistPage.test.tsx | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Frontend/src/FrontendLogic/LogicConnection.ts b/Frontend/src/FrontendLogic/LogicConnection.ts index 0b50100..2ca0644 100644 --- a/Frontend/src/FrontendLogic/LogicConnection.ts +++ b/Frontend/src/FrontendLogic/LogicConnection.ts @@ -1,15 +1,23 @@ export const BACKEND_URL = import.meta.env.VITE_BACKEND_URL ?? 'http://it2810-10.idi.ntnu.no:3001/' +export const NETWORK_ERROR_MESSAGE = + 'Could not reach the server. Check your connection and try again.' + export async function Gql(query: string, variables: Record): Promise { const token = GetStoredToken() - const res = await fetch(BACKEND_URL, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - ...(token ? { Authorization: `Bearer ${token}` } : {}) - }, - body: JSON.stringify({ query, variables }) - }) + let res: Response + try { + res = await fetch(BACKEND_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + ...(token ? { Authorization: `Bearer ${token}` } : {}) + }, + body: JSON.stringify({ query, variables }) + }) + } catch { + throw new Error(NETWORK_ERROR_MESSAGE) + } if (!res.ok) throw new Error(`Request failed: ${res.status}`) const json = await res.json() if (json.errors) throw new Error(json.errors[0].message) diff --git a/Frontend/src/tests/WishlistPage.test.tsx b/Frontend/src/tests/WishlistPage.test.tsx index 5e8dd41..9ed7c97 100644 --- a/Frontend/src/tests/WishlistPage.test.tsx +++ b/Frontend/src/tests/WishlistPage.test.tsx @@ -196,10 +196,10 @@ describe('wishlistLogicFrontend', () => { await expect(FetchWishlistFrontend(1)).rejects.toThrow('first') }) - it('passes network failures on to the caller', async () => { + it('turns network failures into a readable message', async () => { FetchMock.mockRejectedValue(new TypeError('Failed to fetch')) - await expect(FetchWishlistFrontend(1)).rejects.toThrow('Failed to fetch') + await expect(FetchWishlistFrontend(1)).rejects.toThrow('Could not reach the server') }) }) }) From 116eb1a74f27aa28f83fcc8a5582f320d3d9ed53 Mon Sep 17 00:00:00 2001 From: catjoha04-byte Date: Tue, 10 Nov 2026 19:34:55 +0100 Subject: [PATCH 2/8] fix: load errors inside section without duplicates --- Frontend/src/components/GenreGamesSection.tsx | 10 +++- Frontend/src/components/RecentReviews.tsx | 9 +++- Frontend/src/components/Reviews.tsx | 8 +++- Frontend/src/components/TopGames.tsx | 9 +++- Frontend/src/css/GamePage.css | 5 ++ Frontend/src/css/MainPage.css | 14 ++++++ Frontend/src/pages/GamePage.tsx | 10 ++-- Frontend/src/pages/MainPage.tsx | 37 ++++++++------ Frontend/src/pages/ProfilePage.tsx | 48 ++++++++++++------- Frontend/src/pages/WishlistPage.tsx | 9 ++-- Frontend/src/tests/MainPage.test.tsx | 16 +++++-- 11 files changed, 124 insertions(+), 51 deletions(-) diff --git a/Frontend/src/components/GenreGamesSection.tsx b/Frontend/src/components/GenreGamesSection.tsx index 2350ddf..83d1417 100644 --- a/Frontend/src/components/GenreGamesSection.tsx +++ b/Frontend/src/components/GenreGamesSection.tsx @@ -38,6 +38,7 @@ type GenreGamesSectionProps = { title: string genres: string[] genresLoading: boolean + genresError?: string | null loadGames: (genre: string) => Promise loadingText: string emptyText: string @@ -49,6 +50,7 @@ export function GenreGamesSection({ title, genres, genresLoading, + genresError = null, loadGames, loadingText, emptyText, @@ -157,9 +159,13 @@ export function GenreGamesSection({
{isLoading ? (

{loadingText}

+ ) : genresError ? ( +

+ Could not load genres: {genresError} +

) : loadError ? ( -

- {loadError} +

+ Could not load games: {loadError}

) : visibleGames.length > 0 ? ( visibleGames.map((game) => ) diff --git a/Frontend/src/components/RecentReviews.tsx b/Frontend/src/components/RecentReviews.tsx index 0a84cd1..86cd6fd 100644 --- a/Frontend/src/components/RecentReviews.tsx +++ b/Frontend/src/components/RecentReviews.tsx @@ -12,9 +12,10 @@ import type { Review } from '../types/Review' type RecentReviewsProps = { reviews: Review[] isLoading: boolean + error?: string | null } -export default function RecentReviews({ reviews, isLoading }: RecentReviewsProps) { +export default function RecentReviews({ reviews, isLoading, error = null }: RecentReviewsProps) { const recentReviewsRef = useRef(null) const isStackedLayout = useMediaQuery('(max-width: 56rem)') const [visibleReviewCount, setVisibleReviewCount] = useState(0) @@ -73,7 +74,11 @@ export default function RecentReviews({ reviews, isLoading }: RecentReviewsProps
{isLoading ? ( -

Loading reviews...

+

Loading reviews...

+ ) : error ? ( +

+ Could not load recent reviews: {error} +

) : (
)} - {(error ?? loadError) && ( + {error && (

- {error ?? loadError} + {error}

)} @@ -142,6 +142,10 @@ export default function Reviews({ onDelete={HandleDelete} /> )) + ) : loadError ? ( +

+ Could not load reviews: {loadError} +

) : (

No reviews yet.

)} diff --git a/Frontend/src/components/TopGames.tsx b/Frontend/src/components/TopGames.tsx index e9903e7..379efec 100644 --- a/Frontend/src/components/TopGames.tsx +++ b/Frontend/src/components/TopGames.tsx @@ -5,9 +5,10 @@ import PassiveGameCard from './PassiveGameCard' interface TopGamesProps { loading: boolean featuredGames: PageGame[] + error?: string | null } -export default function TopGames({ loading, featuredGames }: TopGamesProps) { +export default function TopGames({ loading, featuredGames, error = null }: TopGamesProps) { return (
@@ -15,7 +16,11 @@ export default function TopGames({ loading, featuredGames }: TopGamesProps) {
{loading ? ( -

Loading games...

+

Loading games...

+ ) : error ? ( +

+ Could not load the top games: {error} +

) : featuredGames.length === 0 ? (

No games found.

) : ( diff --git a/Frontend/src/css/GamePage.css b/Frontend/src/css/GamePage.css index d0b4a67..5295edc 100644 --- a/Frontend/src/css/GamePage.css +++ b/Frontend/src/css/GamePage.css @@ -4,6 +4,11 @@ margin: 0 auto; } +.game-page-message { + margin: 2rem auto; + max-width: 40rem; +} + @media (max-width: 43.75rem) { .game-page { width: min(calc(100% - 2rem), 32rem); diff --git a/Frontend/src/css/MainPage.css b/Frontend/src/css/MainPage.css index cde951e..cc8963a 100644 --- a/Frontend/src/css/MainPage.css +++ b/Frontend/src/css/MainPage.css @@ -177,6 +177,20 @@ gap: 1.25rem; } +.main-page-error { + margin: 0; + padding: 1rem; + border: 1px solid var(--primary-component-color); + border-radius: var(--border-radius); + background-color: var(--primary-background-color); +} + +.genre-games-row .main-page-error { + grid-column: 1 / -1; + padding: 0.5rem; + border: 0; +} + .genre-see-more { display: flex; justify-content: flex-end; diff --git a/Frontend/src/pages/GamePage.tsx b/Frontend/src/pages/GamePage.tsx index 4be295f..2cbffe4 100644 --- a/Frontend/src/pages/GamePage.tsx +++ b/Frontend/src/pages/GamePage.tsx @@ -69,9 +69,11 @@ export default function GamePage() { />
{isLoading ? ( -

Loading game...

+

Loading game...

) : error ? ( -

Error loading game: {error}

+

+ Something went wrong while loading this game: {error} +

) : game ? ( ) : ( -

Game not found.

+

+ We couldn't find that game. It may have been removed, or the link is wrong. +

)}
diff --git a/Frontend/src/pages/MainPage.tsx b/Frontend/src/pages/MainPage.tsx index a2f67fe..a89d960 100644 --- a/Frontend/src/pages/MainPage.tsx +++ b/Frontend/src/pages/MainPage.tsx @@ -23,21 +23,25 @@ export default function MainPage() { const [genres, setGenres] = useState([]) const [isLoadingFeatured, setIsLoadingFeatured] = useState(true) const [isLoadingGenres, setIsLoadingGenres] = useState(true) - const [error, setError] = useState(null) + // One error per section, so each failure is shown inside the box it belongs to + const [featuredError, setFeaturedError] = useState(null) + const [genresError, setGenresError] = useState(null) + const [reviewsError, setReviewsError] = useState(null) const [recentReviews, setRecentReviews] = useState([]) const [isLoadingReviews, setIsLoadingReviews] = useState(true) useEffect(() => { let cancelled = false - const ReportError = (e: unknown, fallback: string) => { - if (!cancelled) setError(e instanceof Error ? e.message : fallback) - } + const ReportError = + (setError: (message: string) => void, fallback: string) => (e: unknown) => { + if (!cancelled) setError(e instanceof Error ? e.message : fallback) + } LoadTop3Games() .then((games) => { if (!cancelled) setFeaturedGames(games.map(ToPageGame)) }) - .catch((e: unknown) => ReportError(e, 'Could not load top games')) + .catch(ReportError(setFeaturedError, 'Could not load top games')) .finally(() => { if (!cancelled) setIsLoadingFeatured(false) }) @@ -46,7 +50,7 @@ export default function MainPage() { .then((loaded) => { if (!cancelled) setGenres([...loaded].sort()) }) - .catch((e: unknown) => ReportError(e, 'Could not load genres')) + .catch(ReportError(setGenresError, 'Could not load genres')) .finally(() => { if (!cancelled) setIsLoadingGenres(false) }) @@ -55,7 +59,7 @@ export default function MainPage() { .then((reviews) => { if (!cancelled) setRecentReviews(reviews) }) - .catch((e: unknown) => ReportError(e, 'Could not load reviews')) + .catch(ReportError(setReviewsError, 'Could not load reviews')) .finally(() => { if (!cancelled) setIsLoadingReviews(false) }) @@ -101,12 +105,11 @@ export default function MainPage() { HaroldTheHare
- {error && ( -

- {error} -

- )} - +
@@ -114,6 +117,7 @@ export default function MainPage() { title="Top 10 Games by Genre" genres={genres} genresLoading={isLoadingGenres} + genresError={genresError} loadGames={LoadTopGamesByGenre} loadingText="Loading games..." emptyText="No games found for this genre." @@ -123,6 +127,7 @@ export default function MainPage() { title="Discounts by Genre" genres={genres} genresLoading={isLoadingGenres} + genresError={genresError} loadGames={LoadDiscountedGames} loadingText="Loading discount games..." emptyText="No discount games found for this genre." @@ -131,7 +136,11 @@ export default function MainPage() { />
- +