Skip to content

Commit 61ae936

Browse files
committed
Improved error handling and logout redirection
1 parent 67b5748 commit 61ae936

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

app/src/routes/(app)/+layout.server.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export const ssr = false;
22

33
import { Api } from "$lib/api.server.js";
44
import { isArticleFilter, type ArticleFilter } from "$lib/types.js";
5-
import { redirect } from "@sveltejs/kit";
5+
import { error, redirect } from "@sveltejs/kit";
6+
import { ResponseError } from "simple-news-client";
67

78
export async function load({ url, depends, locals, fetch }) {
89
if (!locals.sessionId) {
@@ -17,12 +18,24 @@ export async function load({ url, depends, locals, fetch }) {
1718

1819
const api = new Api({ fetch, sessionId: locals.sessionId });
1920

20-
depends("app:feeds");
21-
const feeds = await api.getFeeds();
22-
depends("app:feedGroups");
23-
const feedGroups = await api.getFeedGroups();
24-
depends("app:feedStats");
25-
const feedStats = await api.getFeedStats();
21+
try {
22+
depends("app:feeds");
23+
const feeds = await api.getFeeds();
24+
depends("app:feedGroups");
25+
const feedGroups = await api.getFeedGroups();
26+
depends("app:feedStats");
27+
const feedStats = await api.getFeedStats();
2628

27-
return { feeds, feedGroups, feedStats, articleFilter };
29+
return { feeds, feedGroups, feedStats, articleFilter };
30+
} catch (err) {
31+
if (err instanceof ResponseError) {
32+
if (err.status === 401) {
33+
return redirect(303, `/login?next=${url.pathname}`);
34+
}
35+
36+
return error(err.status, { message: err.message });
37+
}
38+
39+
return error(500, `${err}`);
40+
}
2841
}

client/src/error.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ const defaultMessages: Record<number, string> = {
88

99
export class ResponseError extends Error {
1010
#status: number;
11+
#details: string | undefined;
1112

1213
constructor(status: number, message?: string) {
13-
const parts = [`[${status}]`];
14-
if (message) {
15-
parts.push(message)
16-
} else if (defaultMessages[status]) {
17-
parts.push(defaultMessages[status])
18-
}
19-
20-
super(parts.join(" "));
14+
super(defaultMessages[status] || "Internal Server Error");
15+
this.#details = message;
2116
this.#status = status;
2217
}
2318

2419
get status(): number {
2520
return this.#status;
2621
}
22+
23+
get details(): string | undefined {
24+
return this.#details;
25+
}
2726
}

client/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
import { AccountResponse, SessionResponse } from "simple-news-types";
2323
import { ResponseError } from "./error.js";
2424

25+
export { ResponseError };
26+
2527
export class Client {
2628
#apiHost: string;
2729
#sessionId: string | undefined;

0 commit comments

Comments
 (0)