Skip to content

Commit

Permalink
Merge pull request #54 from aleksasiriski/as/fix/errors
Browse files Browse the repository at this point in the history
fix: error handling
  • Loading branch information
aleksasiriski authored Dec 17, 2024
2 parents 216fd46 + ad452c4 commit 9868705
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 74 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
install:
pnpm i --frozen-lockfile
prod-start:
docker compose -f docker-compose.prod.yml up -d
prod-stop:
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const nameRegExpMsg =

export const indexRegExp = /^\d{1,4}[a-zA-Z]?\/(?:\d{2}|\d{4})(?:[A-Z]{1,3})?$/;
export const indexRegExpMsg =
'String must be in format: 1-4 numbers, optional single character and 2 or 4 numbers (year)';
'String must be in format: 1-4 numbers, optional single character and 2 or 4 numbers (year) and optional 1-3 uppercase letters';

export const uppercaseRegExp = /^[A-Z0-9_\-+\/\\|]*$/;
export const uppercaseRegExp = /^[A-Z0-9_\-+/\\|]*$/;
export const uppercaseRegExpMsg =
'String must only consist of uppercase letters, numbers and special signs (_, -, +, /, \\, |)';

export const lowercaseRegExp = /^[a-z0-9_\-+\/\\|]*$/;
export const lowercaseRegExp = /^[a-z0-9_\-+/\\|]*$/;
export const lowercaseRegExpMsg =
'String must only consist of lowercase letters, numbers and special signs (_, -, +, /, \\, |)';
36 changes: 13 additions & 23 deletions src/routes/(dashboard)/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import { deleteSessionTokenCookie } from '$lib/server/session';

export const load: PageServerLoad = async ({ url }) => {
try {
const limit = 1000;
const offset = 0;

const studentLimit = Math.round(limit / 2);
const employeeLimit = limit - studentLimit;
const studentOffset = Math.round(offset / 2);
const employeeOffset = offset - studentOffset;

const searchQuery = url.searchParams.get('q') ?? undefined;

const studentsP = getStudents(studentLimit, studentOffset, searchQuery);
const employeesP = getEmployees(employeeLimit, employeeOffset, searchQuery);
const studentsP = getStudents(1000, 0, searchQuery);
const employeesP = getEmployees(1000, 0, searchQuery);
const students = await studentsP;
const employees = await employeesP;

Expand Down Expand Up @@ -49,8 +40,9 @@ export const load: PageServerLoad = async ({ url }) => {
persons
};
} catch (err: unknown) {
return fail(400, {
message: `Failed to get students or employees: ${(err as Error).message}`
console.debug(`Failed to get students and employees: ${(err as Error).message}`);
return fail(500, {
message: 'Failed to get students and employees'
});
}
};
Expand All @@ -63,8 +55,8 @@ export const actions: Actions = {
const type = formData.get('type');

if (locals.session === null || locals.user === null) {
return fail(400, {
message: 'Null building or username'
return fail(401, {
message: 'Invalid session'
});
}
const building = locals.session.building;
Expand Down Expand Up @@ -93,34 +85,32 @@ export const actions: Actions = {
await toggleEmployeeState(id, building, creator);
} else {
return fail(400, {
message: 'Invalid type'
message: 'Invalid type (neither student nor employee)'
});
}
} catch (err: unknown) {
const msg = `Failed to toggle state: ${(err as Error).message}`;
console.debug(msg);
console.debug(`Failed to toggle state: ${(err as Error).message}`);
return fail(400, {
message: msg
message: 'Failed to toggle state'
});
}
},
logout: async (event) => {
try {
const formData = await event.request.formData();
const idS = formData.get('id_session');

if (idS === null || idS === undefined || typeof idS !== 'string' || idS === '') {
return fail(400, {
message: 'Invalid or missing fields'
});
}

await invalidateSession(idS);
deleteSessionTokenCookie(event);
} catch (err: unknown) {
const msg = `Failed to toggle state: ${(err as Error).message}`;
console.debug(msg);
console.debug(`Failed to logout: ${(err as Error).message}`);
return fail(400, {
message: msg
message: 'Failed to logout'
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/routes/(dashboard)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import DataTable from './data-table.svelte';
import { columns } from './columns';
let { data } = $props();
let { data, form: actionData } = $props();
const searchQuery = data.searchQuery;
let searchBox: HTMLFormElement | null;
Expand All @@ -28,6 +28,7 @@
<Button type="reset" variant="destructive" size="icon" class="flex-shrink-0">
<Reset />
</Button>
<p class="my-auto text-rose-600 dark:text-rose-500">{actionData?.message}</p>
</div>
</form>
<Separator />
Expand Down
20 changes: 12 additions & 8 deletions src/routes/(dashboard)/create/employee/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, type Actions } from '@sveltejs/kit';
import { superValidate, message } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';

import { createEmployee } from '$lib/server/db/employee';
Expand All @@ -23,13 +23,15 @@ export const actions: Actions = {
const form = await superValidate(request, zod(formSchema));
if (!form.valid) {
return fail(400, {
form
form,
message: 'Invalid form inputs'
});
}

if (locals.session === null || locals.user === null) {
return fail(400, {
form
return fail(401, {
form,
message: 'Invalid session'
});
}

Expand All @@ -39,14 +41,16 @@ export const actions: Actions = {
const creator = locals.user.username;
await createEmployee(email, fname, lname, department, building, creator);
} catch (err: unknown) {
const message = `Failed to create employee: ${(err as Error).message}}`;
console.debug(message);
console.debug(`Failed to create employee: ${(err as Error).message}`);
return fail(400, {
form,
message
message: 'Employee already exists'
});
}

return message(form, 'Employee created successfully!');
return {
form,
message: 'Employee created successfully!'
};
}
};
3 changes: 2 additions & 1 deletion src/routes/(dashboard)/create/employee/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { toast } from 'svelte-sonner';
import { formSchema } from './schema';
let { data } = $props();
let { data, form: actionData } = $props();
const form = superForm(data.form, {
validators: zodClient(formSchema),
Expand All @@ -31,6 +31,7 @@
<Card.Description>
Create an employee who wants to enter the building for the first time.
</Card.Description>
<p class="my-auto text-rose-600 dark:text-rose-500">{actionData?.message}</p>
</Card.Header>
<Card.Content class="grid gap-4">
<Form.Field {form} name="fname">
Expand Down
20 changes: 12 additions & 8 deletions src/routes/(dashboard)/create/student/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, type Actions } from '@sveltejs/kit';
import { superValidate, message } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';

import { createStudent } from '$lib/server/db/student';
Expand All @@ -23,13 +23,15 @@ export const actions: Actions = {
const form = await superValidate(request, zod(formSchema));
if (!form.valid) {
return fail(400, {
form
form,
message: 'Invalid form inputs'
});
}

if (locals.session === null || locals.user === null) {
return fail(400, {
form
return fail(401, {
form,
message: 'Invalid session'
});
}

Expand All @@ -39,14 +41,16 @@ export const actions: Actions = {
const creator = locals.user.username;
await createStudent(index, fname, lname, department, building, creator);
} catch (err: unknown) {
const message = `Failed to create student: ${(err as Error).message}`;
console.debug(message);
console.debug(`Failed to create student: ${(err as Error).message}`);
return fail(400, {
form,
message
message: 'Student already exists'
});
}

return message(form, 'Student created successfully!');
return {
form,
message: 'Student created successfully!'
};
}
};
3 changes: 2 additions & 1 deletion src/routes/(dashboard)/create/student/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { toast } from 'svelte-sonner';
import { formSchema } from './schema';
let { data } = $props();
let { data, form: actionData } = $props();
const form = superForm(data.form, {
validators: zodClient(formSchema),
Expand All @@ -31,6 +31,7 @@
<Card.Description>
Create an student who wants to enter the building for the first time.
</Card.Description>
<p class="my-auto text-rose-600 dark:text-rose-500">{actionData?.message}</p>
</Card.Header>
<Card.Content class="grid gap-4">
<Form.Field {form} name="fname">
Expand Down
15 changes: 9 additions & 6 deletions src/routes/admin/create/building/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, type Actions } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { formSchema } from './schema';
import type { PageServerLoad } from './$types';
Expand All @@ -19,7 +19,8 @@ export const actions: Actions = {
const form = await superValidate(event.request, zod(formSchema));
if (!form.valid) {
return fail(400, {
form
form,
message: 'Invalid form inputs'
});
}

Expand All @@ -36,14 +37,16 @@ export const actions: Actions = {
const { building } = form.data;
await createBuilding(building);
} catch (err: unknown) {
const message = `Failed to create building: ${(err as Error).message}}`;
console.debug(message);
console.debug(`Failed to create building: ${(err as Error).message}`);
return fail(400, {
form,
message
message: 'Building already exists'
});
}

return message(form, 'Building created successfully!');
return {
form,
message: 'Building created successfully!'
};
}
};
3 changes: 2 additions & 1 deletion src/routes/admin/create/building/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { toast } from 'svelte-sonner';
import { formSchema } from './schema';
let { data } = $props();
let { data, form: actionData } = $props();
const form = superForm(data.form, {
validators: zodClient(formSchema),
Expand All @@ -28,6 +28,7 @@
<Card.Header>
<Card.Title class="text-2xl">Create building</Card.Title>
<Card.Description>Enter the name of the new building to create.</Card.Description>
<p class="text-rose-600 dark:text-rose-500">{actionData?.message}</p>
</Card.Header>
<Card.Content class="grid gap-4">
<Form.Field {form} name="building">
Expand Down
15 changes: 9 additions & 6 deletions src/routes/admin/create/department/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, type Actions } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { formSchema } from './schema';
import type { PageServerLoad } from './$types';
Expand All @@ -19,7 +19,8 @@ export const actions: Actions = {
const form = await superValidate(event.request, zod(formSchema));
if (!form.valid) {
return fail(400, {
form
form,
message: 'Invalid form inputs'
});
}

Expand All @@ -36,14 +37,16 @@ export const actions: Actions = {
const { department } = form.data;
await createDepartment(department);
} catch (err: unknown) {
const message = `Failed to create department: ${(err as Error).message}}`;
console.debug(message);
console.debug(`Failed to create department: ${(err as Error).message}`);
return fail(400, {
form,
message
message: 'Department already exists'
});
}

return message(form, 'Department created successfully!');
return {
form,
message: 'Department created successfully!'
};
}
};
3 changes: 2 additions & 1 deletion src/routes/admin/create/department/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { toast } from 'svelte-sonner';
import { formSchema } from './schema';
let { data } = $props();
let { data, form: actionData } = $props();
const form = superForm(data.form, {
validators: zodClient(formSchema),
Expand All @@ -28,6 +28,7 @@
<Card.Header>
<Card.Title class="text-2xl">Create department</Card.Title>
<Card.Description>Enter the name of the new department to create.</Card.Description>
<p class="text-rose-600 dark:text-rose-500">{actionData?.message}</p>
</Card.Header>
<Card.Content class="grid gap-4">
<Form.Field {form} name="department">
Expand Down
12 changes: 7 additions & 5 deletions src/routes/admin/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import { formSchema } from './schema';
export const load: PageServerLoad = async () => {
const form = await superValidate(zod(formSchema));

return { form };
return {
form
};
};

export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event.request, zod(formSchema));
if (!form.valid) {
return fail(400, {
form
form,
message: 'Invalid form inputs'
});
}

Expand All @@ -35,11 +38,10 @@ export const actions: Actions = {
// Create the new user
await createUser(username, password);
} catch (err: unknown) {
const message = `Failed to register: ${(err as Error).message}}`;
console.debug(message);
console.debug(`Failed to register: ${(err as Error).message}`);
return fail(400, {
form,
message
message: 'Username already exists or password is too weak'
});
}
}
Expand Down
Loading

0 comments on commit 9868705

Please sign in to comment.