From f63f5ccc5ab066a6df9dbe268806828cd7a22e93 Mon Sep 17 00:00:00 2001 From: themodrnhakr Date: Mon, 29 Sep 2025 21:13:26 -0500 Subject: [PATCH] Rework data fetching and error handling. Added server-side error handling for obvious total failure cases. If a task is missing or has unexpected duplicates, SveltKit with now throw a built in http error. Added support for parent and child task fetching. Serverside, the raw response object is passed to the client. The client is now able to handle the parent and child response objects. --- src/routes/tasks/[task_id]/+page.server.ts | 26 ++++++++++- src/routes/tasks/[task_id]/+page.svelte | 50 +++++++++++----------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/routes/tasks/[task_id]/+page.server.ts b/src/routes/tasks/[task_id]/+page.server.ts index bae9647..38f6f3f 100644 --- a/src/routes/tasks/[task_id]/+page.server.ts +++ b/src/routes/tasks/[task_id]/+page.server.ts @@ -1,9 +1,33 @@ +import logger from "$lib/server/logger"; import TasksService from "$lib/server/services/tasks"; +import { error } from "@sveltejs/kit"; import type { PageServerData } from "./$types"; export const load: PageServerData = async ({ params }) => { const tasks = new TasksService("internal"); + const taskResult = await tasks.getByTaskId([params.task_id]); + + if (taskResult.status === "failure" || !taskResult.data || taskResult.data.length === 0) { + error(404, `No record for '${params.task_id}'.`); + } + if (taskResult.data.length > 1) { + logger.error(`Mulitple database entries match '${params.task_id}' when there should only be one.`); + error(500, "Internal error. Check the logs."); + } + + const task = taskResult.data[0]; + + const [parent, children] = await Promise.all([ + task.parent + ? tasks.getByDbId([task.parent]) + : Promise.resolve({ status: "ok" }), + tasks.getByParent(task.id), + ]); + return { - task: await tasks.getByTaskId([params.task_id]), + task, + parent, + children, }; }; + diff --git a/src/routes/tasks/[task_id]/+page.svelte b/src/routes/tasks/[task_id]/+page.svelte index db65da9..81eefbc 100644 --- a/src/routes/tasks/[task_id]/+page.svelte +++ b/src/routes/tasks/[task_id]/+page.svelte @@ -1,33 +1,35 @@ -{#if data.task.status === "ok"} - {@const task = data.task.data[0]} - {#if task} -

- {`[[${task.type.prefix}${task.taskId}]] - ${task.description}`} -

-
-

{task.status}

-

{task.priority}

-
-
-

Body

-

{task.body}

-
- {:else} -

Task not found.

- {/if} -{:else if data.task.status === "failure"} -

Error Loading Task

-

{data.task.error}

- {#if data.task.code} -

Error code: {data.task.code}

- {/if} -{/if} +