Skip to content

Commit d48431d

Browse files
committed
refactor(routes): improve conversation page loading and error handling
1 parent 244941c commit d48431d

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/routes/+layout.ts

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const load = async ({ depends, fetch }) => {
5757
avatarUrl: fetch(`${base}/api/v2/assistants/${conv.assistantId}`)
5858
.then((res) => res.json() as Promise<Serialize<Assistant>>)
5959
.then((assistant) => {
60-
console.log(assistant);
6160
if (!assistant.avatar) {
6261
return undefined;
6362
}

src/routes/conversation/[id]/+page.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { base } from "$app/paths";
22
import type { GETConversationResponse } from "$lib/server/api/routes/groups/conversations.js";
33
import { UrlDependency } from "$lib/types/UrlDependency";
4-
import { error } from "@sveltejs/kit";
4+
import { fetchJSON } from "$lib/utils/fetchJSON.js";
5+
import { redirect } from "@sveltejs/kit";
56

67
export const load = async ({ params, depends, fetch }) => {
78
depends(UrlDependency.Conversation);
89

9-
const r = await fetch(`${base}/api/v2/conversations/${params.id}`);
10-
11-
if (!r.ok) {
12-
console.log({ r });
13-
error(r.status, "Failed to fetch conversation");
10+
try {
11+
return await fetchJSON<GETConversationResponse>(`${base}/api/v2/conversations/${params.id}`, {
12+
fetch,
13+
});
14+
} catch {
15+
redirect(302, "/");
1416
}
15-
16-
const data = await r.json();
17-
18-
return data as GETConversationResponse;
1917
};

0 commit comments

Comments
 (0)