diff --git a/frontend/src/app.d.ts b/frontend/src/app.d.ts index 9176095..d4759fd 100644 --- a/frontend/src/app.d.ts +++ b/frontend/src/app.d.ts @@ -1,8 +1,15 @@ // See https://kit.svelte.dev/docs/types#app + +import type { ErrorCode } from "$lib/api/entities"; + // for information about these interfaces declare global { namespace App { - // interface Error {} + interface Error { + code: ErrorCode; + message?: string | undefined; + details?: string | undefined; + } // interface Locals {} // interface PageData {} // interface Platform {} diff --git a/frontend/src/routes/+error.svelte b/frontend/src/routes/+error.svelte index 21dc4d0..9d7d887 100644 --- a/frontend/src/routes/+error.svelte +++ b/frontend/src/routes/+error.svelte @@ -1,17 +1,20 @@

An error occurred ({$page.status})

-{#if $page.status === 404} +{#if $page.error?.code === ErrorCode.NotFound}

The page you were looking for was not found. If you're sure the page exists, check for any typos in the address.

-{:else if $page.status === 429} +{:else if $page.error?.code === ErrorCode.Forbidden || $page.error?.code === ErrorCode.InvalidToken} +

You're not logged in, or you aren't allowed to access this page.

+{:else if $page.error?.code === 429}

You've exceeded a rate limit, please try again later.

-{:else if $page.status === 500} +{:else if $page.error?.code === 500}

An internal error occurred. Please try again later.

If this error keeps happening, please {/if} -

Error message: {$page.error?.message}

+

Error code: {$page.error?.code}

diff --git a/frontend/src/routes/@[username]/+error.svelte b/frontend/src/routes/@[username]/+error.svelte index 8e0cbbc..4f0292f 100644 --- a/frontend/src/routes/@[username]/+error.svelte +++ b/frontend/src/routes/@[username]/+error.svelte @@ -1,22 +1,32 @@ -

An error occurred ({$page.status})

- -{#if $page.status === 404} -

The user you were looking for couldn't be found. Please check for any typos.

-{:else if $page.status === 429} -

You've exceeded a rate limit, please try again later.

-{:else if $page.status === 500} -

An internal error occurred. Please try again later.

+{#if $page.error?.code === ErrorCode.Forbidden || $page.error?.code === ErrorCode.InvalidToken} +

Not logged in

- If this error keeps happening, please file a bug report with an explanation of what you did to cause the error. + Either you aren't logged in, or your login has expired. Please log in again.

-{/if} +{:else} +

An error occurred ({$page.status})

-

Error message: {$page.error?.message}

+ {#if $page.status === 404} +

The user you were looking for couldn't be found. Please check for any typos.

+ {:else if $page.status === 429} +

You've exceeded a rate limit, please try again later.

+ {:else if $page.status === 500} +

An internal error occurred. Please try again later.

+

+ If this error keeps happening, please file a bug report with an explanation of what you did to cause the error. +

+ {/if} + +

Error message: {$page.error?.message}

+{/if} diff --git a/frontend/src/routes/@[username]/+page.server.ts b/frontend/src/routes/@[username]/+page.server.ts index 9a4c26f..447c5e3 100644 --- a/frontend/src/routes/@[username]/+page.server.ts +++ b/frontend/src/routes/@[username]/+page.server.ts @@ -11,7 +11,7 @@ export const load = async ({ params }) => { return resp; } catch (e) { if ((e as APIError).code === ErrorCode.UserNotFound) { - throw error(404, (e as APIError).message); + throw error(404, e as APIError); } throw e; diff --git a/frontend/src/routes/@[username]/+page.svelte b/frontend/src/routes/@[username]/+page.svelte index ca29cbc..76da6de 100644 --- a/frontend/src/routes/@[username]/+page.svelte +++ b/frontend/src/routes/@[username]/+page.svelte @@ -196,7 +196,7 @@

- Your profile is empty! You can customize it by going to the edit profile page. (only you can see this) diff --git a/frontend/src/routes/@[username]/[memberName]/+page.server.ts b/frontend/src/routes/@[username]/[memberName]/+page.server.ts index ab41c47..9b14e70 100644 --- a/frontend/src/routes/@[username]/[memberName]/+page.server.ts +++ b/frontend/src/routes/@[username]/[memberName]/+page.server.ts @@ -14,9 +14,9 @@ export const load = async ({ params }) => { (e as APIError).code === ErrorCode.UserNotFound || (e as APIError).code === ErrorCode.MemberNotFound ) { - throw error(404, (e as APIError).message); + throw error(404, e as APIError); } - throw error(500, (e as APIError).message); + throw error(500, e as APIError); } }; diff --git a/frontend/src/routes/@[username]/edit/+layout.ts b/frontend/src/routes/@[username]/edit/+layout.ts index 894eb83..2fd9c4c 100644 --- a/frontend/src/routes/@[username]/edit/+layout.ts +++ b/frontend/src/routes/@[username]/edit/+layout.ts @@ -1,23 +1,28 @@ import type { PrideFlag, APIError, MeUser, PronounsJson } from "$lib/api/entities"; import { apiFetchClient } from "$lib/api/fetch"; -import { error } from "@sveltejs/kit"; +import { error, redirect, type Redirect } from "@sveltejs/kit"; import pronounsRaw from "$lib/pronouns.json"; const pronouns = pronounsRaw as PronounsJson; export const ssr = false; -export const load = async () => { +export const load = async ({ params }) => { try { const user = await apiFetchClient(`/users/@me`); const flags = await apiFetchClient("/users/@me/flags"); + if (params.username !== user.name) { + throw redirect(303, `/@${user.name}/edit`); + } + return { user, pronouns: pronouns.autocomplete, flags, }; } catch (e) { - throw error((e as APIError).code, (e as APIError).message); + if ("code" in e) throw error(500, e as APIError); + throw e; } }; diff --git a/frontend/src/routes/[username]/+page.server.ts b/frontend/src/routes/[username]/+page.server.ts index 3794610..39331d6 100644 --- a/frontend/src/routes/[username]/+page.server.ts +++ b/frontend/src/routes/[username]/+page.server.ts @@ -11,7 +11,7 @@ export const load = async ({ params }) => { throw redirect(303, `/@${resp.name}`); } catch (e) { if ((e as APIError).code === ErrorCode.UserNotFound) { - throw error(404, (e as APIError).message); + throw error(404, e as APIError); } throw e; diff --git a/frontend/src/routes/edit/member/[id]/+page.svelte b/frontend/src/routes/edit/member/[id]/+page.svelte index 3547165..3799a7d 100644 --- a/frontend/src/routes/edit/member/[id]/+page.svelte +++ b/frontend/src/routes/edit/member/[id]/+page.svelte @@ -768,7 +768,7 @@ Your member list is currently hidden, so this setting has no effect. If you want to make your member list visible again, - edit your user profile. + edit your user profile.
{/if} diff --git a/frontend/src/routes/edit/member/[id]/+page.ts b/frontend/src/routes/edit/member/[id]/+page.ts index a84b94d..eeb532d 100644 --- a/frontend/src/routes/edit/member/[id]/+page.ts +++ b/frontend/src/routes/edit/member/[id]/+page.ts @@ -20,6 +20,6 @@ export const load = async ({ params }) => { flags, }; } catch (e) { - throw error((e as APIError).code, (e as APIError).message); + throw error((e as APIError).code, e as APIError); } }; diff --git a/frontend/src/routes/edit/profile/+page.ts b/frontend/src/routes/edit/profile/+page.ts index 1e8be0c..d1b7381 100644 --- a/frontend/src/routes/edit/profile/+page.ts +++ b/frontend/src/routes/edit/profile/+page.ts @@ -1,11 +1,17 @@ -import type { MeUser } from "$lib/api/entities"; +import { ErrorCode, type APIError, type MeUser } from "$lib/api/entities"; import { apiFetchClient } from "$lib/api/fetch"; -import { redirect } from "@sveltejs/kit"; +import { error, redirect } from "@sveltejs/kit"; export const ssr = false; export const load = async () => { - const resp = await apiFetchClient(`/users/@me`); + try { + const resp = await apiFetchClient(`/users/@me`); - throw redirect(303, `/@${resp.name}/edit`); + throw redirect(303, `/@${resp.name}/edit`); + } catch (e) { + if ((e as APIError).code === ErrorCode.Forbidden || (e as APIError).code === ErrorCode.InvalidToken) { + throw error(403, e as APIError) + } + } }; diff --git a/frontend/src/routes/pronouns/[...pronouns]/+page.ts b/frontend/src/routes/pronouns/[...pronouns]/+page.ts index 6ab0564..3442f37 100644 --- a/frontend/src/routes/pronouns/[...pronouns]/+page.ts +++ b/frontend/src/routes/pronouns/[...pronouns]/+page.ts @@ -1,6 +1,6 @@ import { error } from "@sveltejs/kit"; import type { PageLoad } from "./$types"; -import type { PronounsJson } from "$lib/api/entities"; +import { ErrorCode, type PronounsJson } from "$lib/api/entities"; import pronounsRaw from "$lib/pronouns.json"; const pronouns = pronounsRaw as PronounsJson; @@ -10,7 +10,7 @@ export const load = (async ({ params }) => { const arr = param.split("/"); if (arr.length === 0 || params.pronouns === "") { - throw error(404, "Pronouns not found"); + throw error(404, { code: ErrorCode.NotFound, message: "Pronouns not found" }); } if (arr.length === 5) { @@ -46,5 +46,5 @@ export const load = (async ({ params }) => { }; } - throw error(404, "Pronouns not found"); + throw error(404, { code: ErrorCode.NotFound, message: "Pronouns not found" }); }) satisfies PageLoad; diff --git a/frontend/src/routes/reports/+page.ts b/frontend/src/routes/reports/+page.ts index 9b519ca..ee1743e 100644 --- a/frontend/src/routes/reports/+page.ts +++ b/frontend/src/routes/reports/+page.ts @@ -10,37 +10,30 @@ export const load = async ({ url }) => { const reporterId = searchParams.get("reporter_id"); const isClosed = searchParams.get("closed") === "true"; - try { - let reports: Report[]; - if (userId) { - const params = new URLSearchParams(); - if (before) params.append("before", before.toString()); + let reports: Report[]; + if (userId) { + const params = new URLSearchParams(); + if (before) params.append("before", before.toString()); - reports = await apiFetchClient( - `/admin/reports/by-user/${userId}?${params.toString()}`, - ); - } else if (reporterId) { - const params = new URLSearchParams(); - if (before) params.append("before", before.toString()); + reports = await apiFetchClient( + `/admin/reports/by-user/${userId}?${params.toString()}`, + ); + } else if (reporterId) { + const params = new URLSearchParams(); + if (before) params.append("before", before.toString()); - reports = await apiFetchClient( - `/admin/reports/by-reporter/${reporterId}?${params.toString()}`, - ); - } else { - const params = new URLSearchParams(); - if (before) params.append("before", before.toString()); - if (isClosed) params.append("closed", "true"); + reports = await apiFetchClient( + `/admin/reports/by-reporter/${reporterId}?${params.toString()}`, + ); + } else { + const params = new URLSearchParams(); + if (before) params.append("before", before.toString()); + if (isClosed) params.append("closed", "true"); - reports = await apiFetchClient(`/admin/reports?${params.toString()}`); - } - - return { before, isClosed, userId, reporterId, reports } as PageLoadData; - } catch (e) { - if ((e as APIError).code === ErrorCode.Forbidden) { - throw error(400, "You're not an admin"); - } - throw e; + reports = await apiFetchClient(`/admin/reports?${params.toString()}`); } + + return { before, isClosed, userId, reporterId, reports } as PageLoadData; }; interface PageLoadData { diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelte index c28872e..ee1f0a3 100644 --- a/frontend/src/routes/settings/+page.svelte +++ b/frontend/src/routes/settings/+page.svelte @@ -158,7 +158,7 @@


- To change your avatar, go to edit profile. + To change your avatar, go to edit profile.

diff --git a/frontend/src/routes/settings/export/+page.ts b/frontend/src/routes/settings/export/+page.ts index 4596125..972bc2f 100644 --- a/frontend/src/routes/settings/export/+page.ts +++ b/frontend/src/routes/settings/export/+page.ts @@ -10,6 +10,6 @@ export const load = async () => { } catch (e) { if ((e as APIError).code === ErrorCode.NotFound) return { exportData: null }; - throw error((e as APIError).code, (e as APIError).message); + throw error(500, e as APIError); } };