fix: change frontend error object to APIError

This commit is contained in:
sam 2023-08-11 16:33:17 +02:00
parent b3e191f01a
commit b2b3fb37ec
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
15 changed files with 90 additions and 66 deletions

View File

@ -1,8 +1,15 @@
// See https://kit.svelte.dev/docs/types#app // See https://kit.svelte.dev/docs/types#app
import type { ErrorCode } from "$lib/api/entities";
// for information about these interfaces // for information about these interfaces
declare global { declare global {
namespace App { namespace App {
// interface Error {} interface Error {
code: ErrorCode;
message?: string | undefined;
details?: string | undefined;
}
// interface Locals {} // interface Locals {}
// interface PageData {} // interface PageData {}
// interface Platform {} // interface Platform {}

View File

@ -1,17 +1,20 @@
<script lang="ts"> <script lang="ts">
import { page } from "$app/stores"; import { page } from "$app/stores";
import { ErrorCode } from "$lib/api/entities";
</script> </script>
<h1>An error occurred ({$page.status})</h1> <h1>An error occurred ({$page.status})</h1>
{#if $page.status === 404} {#if $page.error?.code === ErrorCode.NotFound}
<p> <p>
The page you were looking for was not found. If you're sure the page exists, check for any typos The page you were looking for was not found. If you're sure the page exists, check for any typos
in the address. in the address.
</p> </p>
{:else if $page.status === 429} {:else if $page.error?.code === ErrorCode.Forbidden || $page.error?.code === ErrorCode.InvalidToken}
<p>You're not logged in, or you aren't allowed to access this page.</p>
{:else if $page.error?.code === 429}
<p>You've exceeded a rate limit, please try again later.</p> <p>You've exceeded a rate limit, please try again later.</p>
{:else if $page.status === 500} {:else if $page.error?.code === 500}
<p>An internal error occurred. Please try again later.</p> <p>An internal error occurred. Please try again later.</p>
<p> <p>
If this error keeps happening, please <a If this error keeps happening, please <a
@ -22,4 +25,4 @@
</p> </p>
{/if} {/if}
<p>Error message: <code>{$page.error?.message}</code></p> <p>Error code: <code>{$page.error?.code}</code></p>

View File

@ -1,22 +1,32 @@
<script lang="ts"> <script lang="ts">
import { page } from "$app/stores"; import { page } from "$app/stores";
import { ErrorCode } from "$lib/api/entities";
</script> </script>
<h1>An error occurred ({$page.status})</h1> {#if $page.error?.code === ErrorCode.Forbidden || $page.error?.code === ErrorCode.InvalidToken}
<h1>Not logged in</h1>
{#if $page.status === 404}
<p>The user you were looking for couldn't be found. Please check for any typos.</p>
{:else if $page.status === 429}
<p>You've exceeded a rate limit, please try again later.</p>
{:else if $page.status === 500}
<p>An internal error occurred. Please try again later.</p>
<p> <p>
If this error keeps happening, please <a Either you aren't logged in, or your login has expired. Please <a href="/auth/login"
href="https://codeberg.org/pronounscc/pronouns.cc/issues" >log in again</a
target="_blank" >.
rel="noreferrer">file a bug report</a
> with an explanation of what you did to cause the error.
</p> </p>
{/if} {:else}
<h1>An error occurred ({$page.status})</h1>
<p>Error message: <code>{$page.error?.message}</code></p> {#if $page.status === 404}
<p>The user you were looking for couldn't be found. Please check for any typos.</p>
{:else if $page.status === 429}
<p>You've exceeded a rate limit, please try again later.</p>
{:else if $page.status === 500}
<p>An internal error occurred. Please try again later.</p>
<p>
If this error keeps happening, please <a
href="https://codeberg.org/pronounscc/pronouns.cc/issues"
target="_blank"
rel="noreferrer">file a bug report</a
> with an explanation of what you did to cause the error.
</p>
{/if}
<p>Error message: <code>{$page.error?.message}</code></p>
{/if}

View File

@ -11,7 +11,7 @@ export const load = async ({ params }) => {
return resp; return resp;
} catch (e) { } catch (e) {
if ((e as APIError).code === ErrorCode.UserNotFound) { if ((e as APIError).code === ErrorCode.UserNotFound) {
throw error(404, (e as APIError).message); throw error(404, e as APIError);
} }
throw e; throw e;

View File

@ -196,7 +196,7 @@
<hr /> <hr />
<p> <p>
<em> <em>
Your profile is empty! You can customize it by going to the <a href="/edit/profile" Your profile is empty! You can customize it by going to the <a href="/@{data.name}/edit"
>edit profile</a >edit profile</a
> page.</em > page.</em
> <span class="text-muted">(only you can see this)</span> > <span class="text-muted">(only you can see this)</span>

View File

@ -14,9 +14,9 @@ export const load = async ({ params }) => {
(e as APIError).code === ErrorCode.UserNotFound || (e as APIError).code === ErrorCode.UserNotFound ||
(e as APIError).code === ErrorCode.MemberNotFound (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);
} }
}; };

View File

@ -1,23 +1,28 @@
import type { PrideFlag, APIError, MeUser, PronounsJson } from "$lib/api/entities"; import type { PrideFlag, APIError, MeUser, PronounsJson } from "$lib/api/entities";
import { apiFetchClient } from "$lib/api/fetch"; 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"; import pronounsRaw from "$lib/pronouns.json";
const pronouns = pronounsRaw as PronounsJson; const pronouns = pronounsRaw as PronounsJson;
export const ssr = false; export const ssr = false;
export const load = async () => { export const load = async ({ params }) => {
try { try {
const user = await apiFetchClient<MeUser>(`/users/@me`); const user = await apiFetchClient<MeUser>(`/users/@me`);
const flags = await apiFetchClient<PrideFlag[]>("/users/@me/flags"); const flags = await apiFetchClient<PrideFlag[]>("/users/@me/flags");
if (params.username !== user.name) {
throw redirect(303, `/@${user.name}/edit`);
}
return { return {
user, user,
pronouns: pronouns.autocomplete, pronouns: pronouns.autocomplete,
flags, flags,
}; };
} catch (e) { } catch (e) {
throw error((e as APIError).code, (e as APIError).message); if ("code" in e) throw error(500, e as APIError);
throw e;
} }
}; };

View File

@ -11,7 +11,7 @@ export const load = async ({ params }) => {
throw redirect(303, `/@${resp.name}`); throw redirect(303, `/@${resp.name}`);
} catch (e) { } catch (e) {
if ((e as APIError).code === ErrorCode.UserNotFound) { if ((e as APIError).code === ErrorCode.UserNotFound) {
throw error(404, (e as APIError).message); throw error(404, e as APIError);
} }
throw e; throw e;

View File

@ -768,7 +768,7 @@
<Icon name="exclamation-triangle-fill" aria-hidden /> <Icon name="exclamation-triangle-fill" aria-hidden />
Your member list is currently hidden, so <strong>this setting has no effect</strong>. If Your member list is currently hidden, so <strong>this setting has no effect</strong>. If
you want to make your member list visible again, you want to make your member list visible again,
<a href="/edit/profile">edit your user profile</a>. <a href="/@{data.user.name}/other">edit your user profile</a>.
<br /> <br />
{/if} {/if}
<Icon name="info-circle-fill" aria-hidden /> <Icon name="info-circle-fill" aria-hidden />

View File

@ -20,6 +20,6 @@ export const load = async ({ params }) => {
flags, flags,
}; };
} catch (e) { } catch (e) {
throw error((e as APIError).code, (e as APIError).message); throw error((e as APIError).code, e as APIError);
} }
}; };

View File

@ -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 { apiFetchClient } from "$lib/api/fetch";
import { redirect } from "@sveltejs/kit"; import { error, redirect } from "@sveltejs/kit";
export const ssr = false; export const ssr = false;
export const load = async () => { export const load = async () => {
const resp = await apiFetchClient<MeUser>(`/users/@me`); try {
const resp = await apiFetchClient<MeUser>(`/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)
}
}
}; };

View File

@ -1,6 +1,6 @@
import { error } from "@sveltejs/kit"; import { error } from "@sveltejs/kit";
import type { PageLoad } from "./$types"; 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"; import pronounsRaw from "$lib/pronouns.json";
const pronouns = pronounsRaw as PronounsJson; const pronouns = pronounsRaw as PronounsJson;
@ -10,7 +10,7 @@ export const load = (async ({ params }) => {
const arr = param.split("/"); const arr = param.split("/");
if (arr.length === 0 || params.pronouns === "") { 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) { 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; }) satisfies PageLoad;

View File

@ -10,37 +10,30 @@ export const load = async ({ url }) => {
const reporterId = searchParams.get("reporter_id"); const reporterId = searchParams.get("reporter_id");
const isClosed = searchParams.get("closed") === "true"; const isClosed = searchParams.get("closed") === "true";
try { let reports: Report[];
let reports: Report[]; if (userId) {
if (userId) { const params = new URLSearchParams();
const params = new URLSearchParams(); if (before) params.append("before", before.toString());
if (before) params.append("before", before.toString());
reports = await apiFetchClient<Report[]>( reports = await apiFetchClient<Report[]>(
`/admin/reports/by-user/${userId}?${params.toString()}`, `/admin/reports/by-user/${userId}?${params.toString()}`,
); );
} else if (reporterId) { } else if (reporterId) {
const params = new URLSearchParams(); const params = new URLSearchParams();
if (before) params.append("before", before.toString()); if (before) params.append("before", before.toString());
reports = await apiFetchClient<Report[]>( reports = await apiFetchClient<Report[]>(
`/admin/reports/by-reporter/${reporterId}?${params.toString()}`, `/admin/reports/by-reporter/${reporterId}?${params.toString()}`,
); );
} else { } else {
const params = new URLSearchParams(); const params = new URLSearchParams();
if (before) params.append("before", before.toString()); if (before) params.append("before", before.toString());
if (isClosed) params.append("closed", "true"); if (isClosed) params.append("closed", "true");
reports = await apiFetchClient<Report[]>(`/admin/reports?${params.toString()}`); reports = await apiFetchClient<Report[]>(`/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;
} }
return { before, isClosed, userId, reporterId, reports } as PageLoadData;
}; };
interface PageLoadData { interface PageLoadData {

View File

@ -158,7 +158,7 @@
<p class="text-center"> <p class="text-center">
<FallbackImage width={200} urls={userAvatars(data.user)} alt="Your avatar" /> <FallbackImage width={200} urls={userAvatars(data.user)} alt="Your avatar" />
<br /> <br />
To change your avatar, go to <a href="/edit/profile">edit profile</a>. To change your avatar, go to <a href="/@{data.user.name}/edit">edit profile</a>.
</p> </p>
</div> </div>
</div> </div>

View File

@ -10,6 +10,6 @@ export const load = async () => {
} catch (e) { } catch (e) {
if ((e as APIError).code === ErrorCode.NotFound) return { exportData: null }; 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);
} }
}; };