From 2f66f943907e433b1a0b5033ff2554e9a9308487 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 19 Mar 2023 03:57:20 +0100 Subject: [PATCH] fix: show 404 page if user cannot be found --- frontend/package.json | 1 + frontend/pnpm-lock.yaml | 17 +++++++++----- .../@[username]/[memberName]/+page.server.ts | 22 ++++++++++++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 99d7ef9..c346583 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -19,6 +19,7 @@ "@tailwindcss/typography": "^0.5.9", "@types/luxon": "^3.2.0", "@types/marked": "^4.0.8", + "@types/node": "^18.15.3", "@types/sanitize-html": "^2.8.1", "@typescript-eslint/eslint-plugin": "^5.45.0", "@typescript-eslint/parser": "^5.45.0", diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml index 37f87c7..055219f 100644 --- a/frontend/pnpm-lock.yaml +++ b/frontend/pnpm-lock.yaml @@ -10,6 +10,7 @@ specifiers: '@tailwindcss/typography': ^0.5.9 '@types/luxon': ^3.2.0 '@types/marked': ^4.0.8 + '@types/node': ^18.15.3 '@types/sanitize-html': ^2.8.1 '@typescript-eslint/eslint-plugin': ^5.45.0 '@typescript-eslint/parser': ^5.45.0 @@ -55,6 +56,7 @@ devDependencies: '@tailwindcss/typography': 0.5.9_tailwindcss@3.2.7 '@types/luxon': 3.2.0 '@types/marked': 4.0.8 + '@types/node': 18.15.3 '@types/sanitize-html': 2.8.1 '@typescript-eslint/eslint-plugin': 5.54.1_mlk7dnz565t663n4razh6a6v6i '@typescript-eslint/parser': 5.54.1_ycpbpc6yetojsgtrx3mwntkhsu @@ -70,7 +72,7 @@ devDependencies: tailwindcss: 3.2.7_postcss@8.4.21 tslib: 2.5.0 typescript: 4.9.5 - vite: 4.1.4 + vite: 4.1.4_@types+node@18.15.3 packages: @@ -504,7 +506,7 @@ packages: svelte: 3.55.1 tiny-glob: 0.2.9 undici: 5.20.0 - vite: 4.1.4 + vite: 4.1.4_@types+node@18.15.3 transitivePeerDependencies: - supports-color dev: true @@ -522,7 +524,7 @@ packages: magic-string: 0.29.0 svelte: 3.55.1 svelte-hmr: 0.15.1_svelte@3.55.1 - vite: 4.1.4 + vite: 4.1.4_@types+node@18.15.3 vitefu: 0.2.4_vite@4.1.4 transitivePeerDependencies: - supports-color @@ -569,6 +571,10 @@ packages: resolution: {integrity: sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==} dev: true + /@types/node/18.15.3: + resolution: {integrity: sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==} + dev: true + /@types/pug/2.0.6: resolution: {integrity: sha512-SnHmG9wN1UVmagJOnyo/qkk0Z7gejYxOYYmaAwr5u2yFYfsupN3sg10kyzN8Hep/2zbHxCnsumxOoRIRMBwKCg==} dev: true @@ -2349,7 +2355,7 @@ packages: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} dev: true - /vite/4.1.4: + /vite/4.1.4_@types+node@18.15.3: resolution: {integrity: sha512-3knk/HsbSTKEin43zHu7jTwYWv81f8kgAL99G5NWBcA1LKvtvcVAC4JjBH1arBunO9kQka+1oGbrMKOjk4ZrBg==} engines: {node: ^14.18.0 || >=16.0.0} hasBin: true @@ -2374,6 +2380,7 @@ packages: terser: optional: true dependencies: + '@types/node': 18.15.3 esbuild: 0.16.17 postcss: 8.4.21 resolve: 1.22.1 @@ -2390,7 +2397,7 @@ packages: vite: optional: true dependencies: - vite: 4.1.4 + vite: 4.1.4_@types+node@18.15.3 dev: true /which/2.0.2: diff --git a/frontend/src/routes/@[username]/[memberName]/+page.server.ts b/frontend/src/routes/@[username]/[memberName]/+page.server.ts index 73d9ec2..ab41c47 100644 --- a/frontend/src/routes/@[username]/[memberName]/+page.server.ts +++ b/frontend/src/routes/@[username]/[memberName]/+page.server.ts @@ -1,10 +1,22 @@ import { apiFetch } from "$lib/api/fetch"; -import type { Member } from "$lib/api/entities"; +import { ErrorCode, type APIError, type Member } from "$lib/api/entities"; +import { error } from "@sveltejs/kit"; export const load = async ({ params }) => { - const resp = await apiFetch(`/users/${params.username}/members/${params.memberName}`, { - method: "GET", - }); + try { + const resp = await apiFetch(`/users/${params.username}/members/${params.memberName}`, { + method: "GET", + }); - return resp; + return resp; + } catch (e) { + if ( + (e as APIError).code === ErrorCode.UserNotFound || + (e as APIError).code === ErrorCode.MemberNotFound + ) { + throw error(404, (e as APIError).message); + } + + throw error(500, (e as APIError).message); + } };