fix(frontend): don't throw a 500 error if a user or member doesn't exist

This commit is contained in:
sam 2024-12-03 14:55:41 +01:00
parent 18bdbc0745
commit 71d3b42330
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
2 changed files with 38 additions and 14 deletions

View file

@ -1,13 +1,24 @@
import { apiRequest } from "$api"; 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; const MEMBERS_PER_PAGE = 20;
export const load = async ({ params, fetch, cookies, url }) => { export const load = async ({ params, fetch, cookies, url }) => {
const user = await apiRequest<UserWithMembers>("GET", `/users/${params.username}`, { let user: UserWithMembers;
try {
user = await apiRequest<UserWithMembers>("GET", `/users/${params.username}`, {
fetch, fetch,
cookies, 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 // Paginate members on the server side
let currentPage = 0; let currentPage = 0;

View file

@ -1,7 +1,11 @@
import { apiRequest } from "$api"; import { apiRequest } from "$api";
import ApiError, { ErrorCode } from "$api/error.js";
import type { Member } from "$api/models/member"; 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 }) => { export const load = async ({ params, fetch, cookies }) => {
try {
const member = await apiRequest<Member>( const member = await apiRequest<Member>(
"GET", "GET",
`/users/${params.username}/members/${params.memberName}`, `/users/${params.username}/members/${params.memberName}`,
@ -12,4 +16,13 @@ export const load = async ({ params, 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;
}
}; };