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

@@ -4,6 +4,7 @@ import { DrizzleClient } from "@shared/db/DrizzleClient";
import type { CreateCaseOptions, ClearCaseOptions, SearchCasesFilter } from "@/modules/moderation/moderation.types";
import { getUserWarningEmbed } from "@/modules/moderation/moderation.view";
import { CaseType } from "@shared/lib/constants";
import { SystemError } from "@shared/lib/errors";
export interface ModerationCaseConfig {
dmOnWarn?: boolean;
@@ -100,7 +101,7 @@ export const moderationService = {
});
if (!moderationCase) {
throw new Error("Failed to create moderation case");
throw new SystemError("Failed to create moderation case");
}
const warningCount = await getActiveWarningCount(options.userId);

View File

@@ -2,6 +2,7 @@ import { Collection, Message, PermissionFlagsBits } from "discord.js";
import type { TextBasedChannel } from "discord.js";
import type { PruneOptions, PruneResult, PruneProgress } from "@/modules/moderation/prune.types";
import { config } from "@shared/lib/config";
import { UserError, SystemError } from "@shared/lib/errors";
/**
* Fetch messages from a channel
@@ -30,7 +31,7 @@ async function processBatch(
userId?: string
): Promise<{ deleted: number; skipped: number }> {
if (!('bulkDelete' in channel)) {
throw new Error("This channel type does not support bulk deletion");
throw new UserError("This channel type does not support bulk deletion");
}
// Filter by user if specified
@@ -54,7 +55,7 @@ async function processBatch(
};
} catch (error) {
console.error("Error during bulk delete:", error);
throw new Error("Failed to delete messages");
throw new SystemError("Failed to delete messages");
}
}
@@ -76,12 +77,12 @@ export const pruneService = {
): Promise<PruneResult> {
// Validate channel permissions
if (!('permissionsFor' in channel)) {
throw new Error("Cannot check permissions for this channel type");
throw new UserError("Cannot check permissions for this channel type");
}
const permissions = channel.permissionsFor(channel.client.user!);
if (!permissions?.has(PermissionFlagsBits.ManageMessages)) {
throw new Error("Missing permission to manage messages in this channel");
throw new UserError("Missing permission to manage messages in this channel");
}
const { amount, userId, all } = options;