refactor(modules): propagate UserError in quest, trade, and class services

This commit is contained in:
syntaxbullet
2025-12-22 12:58:47 +01:00
parent 75e586cee8
commit fbcac51370
3 changed files with 14 additions and 10 deletions

View File

@@ -1,7 +1,8 @@
```typescript
import { classes, users } from "@/db/schema";
import { eq, sql } from "drizzle-orm";
import { DrizzleClient } from "@/lib/DrizzleClient";
import { UserError } from "@/lib/errors";
export const classService = {
getAllClasses: async () => {
@@ -14,7 +15,7 @@ export const classService = {
where: eq(classes.id, classId),
});
if (!cls) throw new Error("Class not found");
if (!cls) throw new UserError("Class not found");
const [user] = await txFn.update(users)
.set({ classId: classId })
@@ -37,15 +38,15 @@ export const classService = {
where: eq(classes.id, classId),
});
if (!cls) throw new Error("Class not found");
if (!cls) throw new UserError("Class not found");
if (amount < 0n && (cls.balance ?? 0n) < -amount) {
throw new Error("Insufficient class funds");
if ((cls.balance ?? 0n) < amount) {
throw new UserError("Insufficient class funds");
}
const [updatedClass] = await txFn.update(classes)
.set({
balance: sql`${classes.balance} + ${amount}`,
balance: sql`${ classes.balance } + ${ amount } `,
})
.where(eq(classes.id, classId))
.returning();