Update error handling and logging.
This commit is contained in:
parent
b2d7b4983a
commit
bee3fdf2ca
@ -93,7 +93,9 @@ class TasksService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async upsert(taskData: NewTask): Promise<ServiceResponse<{ id: number }, "INTERNAL_ERROR" | "VALIDATION_ERROR">> {
|
public async upsert(
|
||||||
|
taskData: NewTask,
|
||||||
|
): Promise<ServiceResponse<{ id: number }, "INTERNAL_ERROR" | "MISSING_DATABASE_ENTRY" | "RECORD_CREATION_FAILURE">> {
|
||||||
try {
|
try {
|
||||||
if (taskData.id) {
|
if (taskData.id) {
|
||||||
const updated = await this.db.update(tasks)
|
const updated = await this.db.update(tasks)
|
||||||
@ -101,7 +103,12 @@ class TasksService {
|
|||||||
.where(eq(tasks.id, taskData.id))
|
.where(eq(tasks.id, taskData.id))
|
||||||
.returning({ id: tasks.id });
|
.returning({ id: tasks.id });
|
||||||
if (updated.length === 0) {
|
if (updated.length === 0) {
|
||||||
return { status: "failure", code: "VALIDATION_ERROR", error: `Task with ID ${taskData.id} not found for update.` };
|
logger.error({ msg: `Failed updating ${taskData.id}.`, data: taskData });
|
||||||
|
return {
|
||||||
|
status: "failure",
|
||||||
|
code: "MISSING_DATABASE_ENTRY",
|
||||||
|
error: `Unable to update ${taskData.id}. Likely no associated database entry exist.`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return { status: "ok", data: { id: updated[0].id } };
|
return { status: "ok", data: { id: updated[0].id } };
|
||||||
} else {
|
} else {
|
||||||
@ -109,14 +116,22 @@ class TasksService {
|
|||||||
.values(taskData)
|
.values(taskData)
|
||||||
.returning({ id: tasks.id });
|
.returning({ id: tasks.id });
|
||||||
if (created.length === 0) {
|
if (created.length === 0) {
|
||||||
throw new Error("Insert operation failed to return the new task.");
|
logger.error({ msg: "Failed creating task record.", data: taskData });
|
||||||
|
return {
|
||||||
|
status: "failure",
|
||||||
|
code: "RECORD_CREATION_FAILURE",
|
||||||
|
error: "The database was unable to insert the requested record.",
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return { status: "ok", data: { id: created[0].id } };
|
return { status: "ok", data: { id: created[0].id } };
|
||||||
}
|
}
|
||||||
}
|
} catch (error) {
|
||||||
catch (error) {
|
logger.error(error, "Error upserting task.");
|
||||||
logger.error({ msg: "Error upserting task.", error });
|
return {
|
||||||
return { status: "failure", error: "An internal server error occurred while saving the task.", code: "INTERNAL_ERROR" };
|
status: "failure",
|
||||||
|
error: "An internal server error occurred while trying to save the task.",
|
||||||
|
code: "INTERNAL_ERROR",
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user