diff options
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/+page.server.ts | 10 | ||||
| -rw-r--r-- | src/routes/grocery/[id]/+page.server.ts | 10 |
2 files changed, 18 insertions, 2 deletions
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index fe9b4e3..008f7f3 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -2,6 +2,7 @@ import { fail } from '@sveltejs/kit'; import { loadOverview } from '$lib/server/services/routines'; import { add as addProject } from '$lib/server/services/projects'; import { parseFormString } from '$lib/server/services/validation'; +import { isUniqueViolation } from '$lib/server/db/errors'; import type { Actions, PageServerLoad } from './$types'; export const load: PageServerLoad = async () => { @@ -14,7 +15,14 @@ export const actions: Actions = { if (!name) { return fail(400, { error: 'Le nom est requis' }); } - await addProject(name); + try { + await addProject(name); + } catch (error) { + if (isUniqueViolation(error)) { + return fail(400, { error: 'Ce projet existe déjà' }); + } + throw error; + } return { success: true }; }, }; diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts index a4ad377..232568c 100644 --- a/src/routes/grocery/[id]/+page.server.ts +++ b/src/routes/grocery/[id]/+page.server.ts @@ -16,6 +16,7 @@ import { parseFormString, parseOptionalNumber, } from '$lib/server/services/validation'; +import { isUniqueViolation } from '$lib/server/db/errors'; import type { Category, Grocery, Reference } from '$lib/server/services/types'; import type { Actions, PageServerLoad } from './$types'; @@ -86,7 +87,14 @@ export const actions: Actions = { return fail(400, { error: 'Le nom est requis' }); } - await updateGrocery(groceryId, name, categoryId); + try { + await updateGrocery(groceryId, name, categoryId); + } catch (error) { + if (isUniqueViolation(error)) { + return fail(400, { error: 'Un article porte déjà ce nom' }); + } + throw error; + } return { success: true, type: 'update' as const }; }, |
