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

View File

@@ -1,6 +1,7 @@
```typescript
import { quests, userQuests, users } from "@/db/schema"; import { quests, userQuests, users } from "@/db/schema";
import { eq, and, sql } from "drizzle-orm"; import { eq, and, sql } from "drizzle-orm";
import { UserError } from "@/lib/errors";
import { DrizzleClient } from "@/lib/DrizzleClient"; import { DrizzleClient } from "@/lib/DrizzleClient";
import { economyService } from "@/modules/economy/economy.service"; import { economyService } from "@/modules/economy/economy.service";
import { levelingService } from "@/modules/leveling/leveling.service"; import { levelingService } from "@/modules/leveling/leveling.service";
@@ -45,8 +46,8 @@ export const questService = {
} }
}); });
if (!userQuest) throw new Error("Quest not assigned"); if (!userQuest) throw new UserError("Quest not assigned");
if (userQuest.completedAt) throw new Error("Quest already completed"); if (userQuest.completedAt) throw new UserError("Quest already completed");
// Mark completed // Mark completed
await txFn.update(userQuests) await txFn.update(userQuests)

View File

@@ -1,5 +1,7 @@
import type { TradeSession, TradeParticipant } from "./trade.types"; import type { TradeSession, TradeParticipant } from "./trade.types";
import { DrizzleClient } from "@/lib/DrizzleClient"; import { DrizzleClient } from "@/lib/DrizzleClient";
import { withTransaction } from "@/lib/db";
import { UserError } from "@/lib/errors";
import { economyService } from "@/modules/economy/economy.service"; import { economyService } from "@/modules/economy/economy.service";
import { inventoryService } from "@/modules/inventory/inventory.service"; import { inventoryService } from "@/modules/inventory/inventory.service";
import { itemTransactions } from "@/db/schema"; import { itemTransactions } from "@/db/schema";