fix error handling
This commit is contained in:
@@ -4,8 +4,8 @@ import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { printJobs, users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS, guard } from "@/utils/guard";
|
||||
import strings from "@/utils/strings";
|
||||
import { type InferInsertModel, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
@@ -13,7 +13,9 @@ export async function createPrintJob(printJob: InferInsertModel<typeof printJobs
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -22,7 +24,9 @@ export async function createPrintJob(printJob: InferInsertModel<typeof printJobs
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -32,7 +36,9 @@ export async function createPrintJob(printJob: InferInsertModel<typeof printJobs
|
||||
|
||||
return result[0].jobId;
|
||||
} catch (error) {
|
||||
throw new Error("Druckauftrag konnte nicht hinzugefügt werden.");
|
||||
return {
|
||||
error: "Druckauftrag konnte nicht hinzugefügt werden.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +46,9 @@ export async function createPrintJob(printJob: InferInsertModel<typeof printJobs
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, is, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION
|
||||
}
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -49,7 +57,9 @@ export async function createPrintJob(printJob: InferInsertModel<typeof printJobs
|
||||
});
|
||||
|
||||
if (guard(dbUser, is, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION
|
||||
}
|
||||
}
|
||||
|
||||
await db.update(printJobs).set(printJob).where(eq(printJobs.id, jobId));
|
||||
@@ -59,7 +69,9 @@ export async function abortPrintJob(jobId: string, reason: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -68,7 +80,9 @@ export async function abortPrintJob(jobId: string, reason: string) {
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
@@ -77,22 +91,26 @@ export async function abortPrintJob(jobId: string, reason: string) {
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
return {
|
||||
error: "Druckauftrag nicht gefunden",
|
||||
};
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
return { error: "Druckauftrag wurde bereits abgebrochen" };
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
return { error: "Druckauftrag ist bereits abgeschlossen" };
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id && dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get duration in minutes since startAt
|
||||
@@ -115,7 +133,9 @@ export async function earlyFinishPrintJob(jobId: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -124,7 +144,9 @@ export async function earlyFinishPrintJob(jobId: string) {
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
@@ -133,22 +155,24 @@ export async function earlyFinishPrintJob(jobId: string) {
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
return { error: "Druckauftrag nicht gefunden" };
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
return { error: "Druckauftrag wurde bereits abgebrochen" };
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
return { error: "Druckauftrag ist bereits abgeschlossen" };
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id && dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get duration in minutes since startAt
|
||||
@@ -169,7 +193,9 @@ export async function extendPrintJob(jobId: string, minutes: number, hours: numb
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -178,7 +204,9 @@ export async function extendPrintJob(jobId: string, minutes: number, hours: numb
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
@@ -187,22 +215,24 @@ export async function extendPrintJob(jobId: string, minutes: number, hours: numb
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
return { error: "Druckauftrag nicht gefunden" };
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
return { error: "Druckauftrag wurde bereits abgebrochen" };
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
return { error: "Druckauftrag ist bereits abgeschlossen" };
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id && dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const duration = minutes + hours * 60;
|
||||
@@ -222,7 +252,9 @@ export async function updatePrintComments(jobId: string, comments: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -231,7 +263,9 @@ export async function updatePrintComments(jobId: string, comments: string) {
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
@@ -240,22 +274,24 @@ export async function updatePrintComments(jobId: string, comments: string) {
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
return { error: "Druckauftrag nicht gefunden" };
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
return { error: "Druckauftrag wurde bereits abgebrochen" };
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
return { error: "Druckauftrag ist bereits abgeschlossen" };
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id && dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
await db
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
"use server";
|
||||
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { printers, users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS_NOT, guard } from "@/utils/guard";
|
||||
import strings from "@/utils/strings";
|
||||
import { type InferInsertModel, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
@@ -13,7 +12,9 @@ export async function createPrinter(printer: InferInsertModel<typeof printers>)
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -22,17 +23,23 @@ export async function createPrinter(printer: InferInsertModel<typeof printers>)
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
if (!printer) {
|
||||
throw new Error("Druckerdaten sind erforderlich.");
|
||||
return {
|
||||
error: "Druckerdaten sind erforderlich.",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await db.insert(printers).values(printer);
|
||||
} catch (error) {
|
||||
throw new Error("Drucker konnte nicht hinzugefügt werden.");
|
||||
return {
|
||||
error: "Drucker konnte nicht hinzugefügt werden.",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
@@ -42,7 +49,9 @@ export async function updatePrinter(id: string, data: InferInsertModel<typeof pr
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -51,17 +60,23 @@ export async function updatePrinter(id: string, data: InferInsertModel<typeof pr
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
throw new Error("Druckerdaten sind erforderlich.");
|
||||
return {
|
||||
error: "Druckerdaten sind erforderlich.",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await db.update(printers).set(data).where(eq(printers.id, id));
|
||||
} catch (error) {
|
||||
throw new Error("Drucker konnte nicht aktualisiert werden.");
|
||||
return {
|
||||
error: "Druckerdaten sind erforderlich.",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
@@ -71,7 +86,9 @@ export async function deletePrinter(id: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -80,16 +97,22 @@ export async function deletePrinter(id: string) {
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await db.delete(printers).where(eq(printers.id, id));
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(error.message);
|
||||
return {
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
throw new Error("Ein unbekannter Fehler ist aufgetreten.");
|
||||
return {
|
||||
error: "Ein unbekannter Fehler ist aufgetreten.",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
|
||||
@@ -5,8 +5,8 @@ import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS, IS_NOT, guard } from "@/utils/guard";
|
||||
import strings from "@/utils/strings";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import type { z } from "zod";
|
||||
@@ -18,7 +18,9 @@ export async function updateUser(userId: string, data: z.infer<typeof formSchema
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -27,7 +29,9 @@ export async function updateUser(userId: string, data: z.infer<typeof formSchema
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
await db.update(users).set(data).where(eq(users.id, userId));
|
||||
@@ -42,7 +46,9 @@ export async function deleteUser(userId: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
@@ -51,7 +57,9 @@ export async function deleteUser(userId: string) {
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
return {
|
||||
error: strings.ERROR.PERMISSION,
|
||||
};
|
||||
}
|
||||
|
||||
const targetUser = await db.query.users.findFirst({
|
||||
@@ -59,11 +67,11 @@ export async function deleteUser(userId: string) {
|
||||
});
|
||||
|
||||
if (!targetUser) {
|
||||
throw new Error("Benutzer nicht gefunden");
|
||||
return { error: "Benutzer nicht gefunden" };
|
||||
}
|
||||
|
||||
if (guard(targetUser, IS, UserRole.ADMIN)) {
|
||||
throw new Error("Kann keinen Admin löschen");
|
||||
return { error: "Kann keinen Admin löschen" };
|
||||
}
|
||||
|
||||
await db.delete(users).where(eq(users.id, userId));
|
||||
|
||||
Reference in New Issue
Block a user