From 71d3b42330244987ee147a9ddf3b8cea5e0d9e8b Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 3 Dec 2024 14:55:41 +0100 Subject: [PATCH] fix(frontend): don't throw a 500 error if a user or member doesn't exist --- .../src/routes/@[username]/+page.server.ts | 21 ++++++++++--- .../@[username]/[memberName]/+page.server.ts | 31 +++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Foxnouns.Frontend/src/routes/@[username]/+page.server.ts b/Foxnouns.Frontend/src/routes/@[username]/+page.server.ts index 99e7359..9ae8090 100644 --- a/Foxnouns.Frontend/src/routes/@[username]/+page.server.ts +++ b/Foxnouns.Frontend/src/routes/@[username]/+page.server.ts @@ -1,13 +1,24 @@ import { apiRequest } from "$api"; -import type { PartialMember, UserWithMembers } from "$api/models"; +import ApiError, { ErrorCode } from "$api/error.js"; +import type { PartialMember, User, UserWithMembers } from "$api/models"; +import log from "$lib/log.js"; +import { error } from "@sveltejs/kit"; const MEMBERS_PER_PAGE = 20; export const load = async ({ params, fetch, cookies, url }) => { - const user = await apiRequest("GET", `/users/${params.username}`, { - fetch, - cookies, - }); + let user: UserWithMembers; + + try { + user = await apiRequest("GET", `/users/${params.username}`, { + fetch, + cookies, + }); + } catch (e) { + if (e instanceof ApiError && e.code === ErrorCode.UserNotFound) error(404, "User not found"); + log.error("Error fetching user %s:", params.username, e); + throw e; + } // Paginate members on the server side let currentPage = 0; diff --git a/Foxnouns.Frontend/src/routes/@[username]/[memberName]/+page.server.ts b/Foxnouns.Frontend/src/routes/@[username]/[memberName]/+page.server.ts index f3f8400..ee0ed44 100644 --- a/Foxnouns.Frontend/src/routes/@[username]/[memberName]/+page.server.ts +++ b/Foxnouns.Frontend/src/routes/@[username]/[memberName]/+page.server.ts @@ -1,15 +1,28 @@ import { apiRequest } from "$api"; +import ApiError, { ErrorCode } from "$api/error.js"; import type { Member } from "$api/models/member"; +import log from "$lib/log.js"; +import { error } from "@sveltejs/kit"; export const load = async ({ params, fetch, cookies }) => { - const member = await apiRequest( - "GET", - `/users/${params.username}/members/${params.memberName}`, - { - fetch, - cookies, - }, - ); + try { + const member = await apiRequest( + "GET", + `/users/${params.username}/members/${params.memberName}`, + { + fetch, + cookies, + }, + ); - return { member }; + return { member }; + } catch (e) { + if (e instanceof ApiError) { + if (e.code === ErrorCode.UserNotFound) error(404, "User not found"); + if (e.code === ErrorCode.MemberNotFound) error(404, "Member not found"); + } + + log.error("Error fetching user %s/member %s:", params.username, params.memberName, e); + throw e; + } };