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.
This commit is contained in:
themodrnhakr 2025-09-29 21:13:26 -05:00
parent 19ff88c661
commit f63f5ccc5a
2 changed files with 51 additions and 25 deletions

View File

@ -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,
};
};

View File

@ -1,33 +1,35 @@
<script lang="ts">
import type { Task } from "$lib/server/services/tasks";
import TaskView from "$lib/ui/Tasks/TaskView.svelte";
import type { PageProps } from "./$types";
let { data }: PageProps = $props();
console.log(data);
const parentTask: Task | null =
(data.parent?.status === "ok" && data.parent.data)
? data.parent.data[0] ?? null
: null;
const childrenTask: Task[] =
(data.children.status === "ok" && data.children.data)
? data.children.data
: [];
const task = data.task;
const parent = {
prefix: parentTask?.type.prefix ?? null,
taskId: parentTask?.taskId ?? null,
description: parentTask?.description ?? null,
};
const children = childrenTask.map(x => ({
prefix: x.type.prefix,
taskId: x.taskId,
description: x.description,
status: x.status,
}));
</script>
{#if data.task.status === "ok"}
{@const task = data.task.data[0]}
{#if task}
<h1>
{`[[${task.type.prefix}${task.taskId}]] - ${task.description}`}
</h1>
<div class="container">
<p><strong>{task.status}</strong></p>
<p><strong>{task.priority}</strong></p>
</div>
<div>
<h2>Body</h2>
<p>{task.body}</p>
</div>
{:else}
<p>Task not found.</p>
{/if}
{:else if data.task.status === "failure"}
<h2>Error Loading Task</h2>
<p>{data.task.error}</p>
{#if data.task.code}
<p>Error code: {data.task.code}</p>
{/if}
{/if}
<TaskView {task} {parent} {children} />
<style>
.container {