fix: standardize error classes in shared service modules

Replace raw `Error` with `UserError` for user-facing conditions (invalid trade state, user not found, permission/channel type checks) and `SystemError` for internal failures (DB insert failures, external API errors, missing config). Improves Discord UX by ensuring user-facing errors are surfaced cleanly via withCommandErrorHandling.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
syntaxbullet
2026-03-18 12:51:16 +01:00
parent 22e446ff28
commit a96c6caa49
8 changed files with 32 additions and 27 deletions

View File

@@ -96,7 +96,7 @@ export const economyService = {
});
if (!user) {
throw new Error("User not found");
throw new UserError("User not found");
}
let streak = (user.dailyStreak || 0) + 1;

View File

@@ -4,7 +4,7 @@ import { TimerType, TransactionType } from "@shared/lib/constants";
import { config } from "@shared/lib/config";
import { withTransaction } from "@/lib/db";
import type { Transaction } from "@shared/lib/types";
import { UserError } from "@shared/lib/errors";
import { UserError, SystemError } from "@shared/lib/errors";
const EXAM_TIMER_TYPE = TimerType.EXAM_SYSTEM;
const EXAM_TIMER_KEY = 'default';
@@ -86,7 +86,7 @@ export const examService = {
// Ensure user exists
const { userService } = await import("@shared/modules/user/user.service");
const user = await userService.getOrCreateUser(userId, username, txFn);
if (!user) throw new Error("Failed to get or create user.");
if (!user) throw new SystemError("Failed to get or create user.");
const now = new Date();
const currentDay = now.getDay();
@@ -126,7 +126,7 @@ export const examService = {
where: eq(users.id, BigInt(userId))
});
if (!user) throw new Error("User not found");
if (!user) throw new UserError("User not found");
const timer = await txFn.query.userTimers.findFirst({
where: and(