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; + } };