fix(frontend): don't throw a 500 error if a user or member doesn't exist
This commit is contained in:
parent
18bdbc0745
commit
71d3b42330
2 changed files with 38 additions and 14 deletions
|
@ -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<UserWithMembers>("GET", `/users/${params.username}`, {
|
||||
let user: UserWithMembers;
|
||||
|
||||
try {
|
||||
user = await apiRequest<UserWithMembers>("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;
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
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 }) => {
|
||||
try {
|
||||
const member = await apiRequest<Member>(
|
||||
"GET",
|
||||
`/users/${params.username}/members/${params.memberName}`,
|
||||
|
@ -12,4 +16,13 @@ export const load = async ({ params, fetch, cookies }) => {
|
|||
);
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue