Add: Use fail() to indicate action error
This commit is contained in:
parent
25324d17a8
commit
e5a4572856
@ -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);
|
||||
|
||||
if ('error' in result) {
|
||||
runIfSuccess(result, (success) => {
|
||||
redirect(303, `/posts/${success.result}`);
|
||||
});
|
||||
|
||||
} else {
|
||||
redirect(303, `/posts/${result.result}`);
|
||||
}
|
||||
return runIfError(result, (error) => {
|
||||
return errorToFail(error);
|
||||
});
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
|
||||
@ -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} */
|
||||
|
||||
@ -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} */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user