From e5a4572856c4d2f5588ea0ea4914e27e6f908da0 Mon Sep 17 00:00:00 2001 From: bloodwiing Date: Mon, 20 May 2024 12:15:16 +0300 Subject: [PATCH] Add: Use fail() to indicate action error --- src/routes/(app)/compose/+page.server.js | 40 ++++++++++++++------- src/routes/(entry)/login/+page.server.js | 17 +++++---- src/routes/(entry)/register/+page.server.js | 17 +++++---- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/routes/(app)/compose/+page.server.js b/src/routes/(app)/compose/+page.server.js index 6b66a98..5e0cd89 100644 --- a/src/routes/(app)/compose/+page.server.js +++ b/src/routes/(app)/compose/+page.server.js @@ -3,6 +3,7 @@ import { getCategories, getCategoriesCached } from '$lib/server/db/category.js'; import { createPost } from '$lib/server/db/post.js'; import { getUserIDOfSession } from '$lib/server/db/user.js'; import { parseIntNull } from '$lib/util.js'; +import { errorToFail, getError, getSuccess, runIfError, runIfSuccess } from '$lib/util.server.js'; import { error, fail, redirect } from '@sveltejs/kit'; export async function load({ cookies }) { @@ -22,7 +23,11 @@ async function POST({ request, cookies }) { const userToken = cookies.get('token'); if (!userToken) { - error(401, 'Need to be logged in!'); + return fail(401, { + error: true, + title: 'Invalid session', + msg: 'Need to be logged in to perform this operation', + }); } const data = await request.formData(); @@ -31,29 +36,40 @@ async function POST({ request, cookies }) { const content = data.get('content')?.toString(); if (!categoryId) { - error(400, `Invalid category ID ${categoryId}`); + return fail(400, { + error: true, + title: 'Bad data', + msg: `Invalid category ID ${categoryId}`, + }); } if (!name || !content) { - // return { - // error: true, - // } /** TODO: use fail() */ - return fail(400, {reason: `Not all fields have been filled out`}); + return fail(400, { + error: true, + title: 'Bad data', + msg: 'Name and Content must have content', + }); } const category = (await getCategoriesCached([categoryId])).get(categoryId); if (!category) { - error(400, `Invalid category ID ${categoryId}`); + return fail(400, { + error: true, + title: 'Bad data', + msg: `Invalid category ID ${categoryId}`, + }); } const result = await createPost(userToken, category, name, content); + + runIfSuccess(result, (success) => { + redirect(303, `/posts/${success.result}`); + }); - if ('error' in result) { - - } else { - redirect(303, `/posts/${result.result}`); - } + return runIfError(result, (error) => { + return errorToFail(error); + }); } export const actions = { diff --git a/src/routes/(entry)/login/+page.server.js b/src/routes/(entry)/login/+page.server.js index 4c141c5..5d07760 100644 --- a/src/routes/(entry)/login/+page.server.js +++ b/src/routes/(entry)/login/+page.server.js @@ -1,5 +1,6 @@ import { createUserSession } from "$lib/server/db/user"; -import { redirect } from "@sveltejs/kit"; +import { errorToFail, runIfError } from "$lib/util.server"; +import { fail, redirect } from "@sveltejs/kit"; /** @type {import("@sveltejs/kit").ServerLoad} */ export async function load({ cookies }) { @@ -10,16 +11,16 @@ export async function load({ cookies }) { /** @type {import("@sveltejs/kit").Action} */ async function POST({ request, cookies }) { - if (request.method !== 'POST') { - return; - } - const data = await request.formData(); const username = data.get('username')?.toString(); const password = data.get('password')?.toString(); if (!username || !password) { - return; + return fail(400, { + error: true, + title: 'Bad data', + msg: 'Username and Password cannot be empty', + }); } const result = await createUserSession(username, password); @@ -32,6 +33,10 @@ async function POST({ request, cookies }) { redirect(302, '/posts'); } + + return runIfError(result, (error) => { + return errorToFail(error); + }); } /** @type {import("@sveltejs/kit").Actions} */ diff --git a/src/routes/(entry)/register/+page.server.js b/src/routes/(entry)/register/+page.server.js index fb058ff..1d763eb 100644 --- a/src/routes/(entry)/register/+page.server.js +++ b/src/routes/(entry)/register/+page.server.js @@ -1,5 +1,6 @@ import { createUser } from '$lib/server/db/user'; -import { redirect } from '@sveltejs/kit'; +import { errorToFail, runIfError } from '$lib/util.server'; +import { fail, redirect } from '@sveltejs/kit'; /** @type {import('@sveltejs/kit').ServerLoad} */ export async function load({ cookies }) { @@ -10,21 +11,23 @@ export async function load({ cookies }) { /** @type {import('@sveltejs/kit').Action} */ async function POST({ request }) { - if (request.method !== 'POST') { - return; - } - const data = await request.formData(); const username = data.get('username')?.toString(); const password = data.get('password')?.toString(); if (!username || !password) { - return; + return fail(400, { + error: true, + title: 'Bad data', + msg: 'Username and Password cannot be empty', + }); } const result = await createUser(username, password); - console.log(result); + return runIfError(result, (error) => { + return errorToFail(error); + }); } /** @type {import('@sveltejs/kit').Actions} */