import {CommandContext} from "slash-create"; import {DatabaseError} from "pg"; export enum ErrorCodes { BAD_CHANNEL_ADMIN = "VGBCA", BAD_CHANNEL_GAME = "VGBCG", BAD_GUILD_ADMIN = "VGBGA", BAD_GUILD_GAME = "VGBGG", NOT_YET_JOINED = "VGNYJ", NOT_ENOUGH_CURRENCY = "VGNEC", DAILY_ALREADY_USED = "VGDLY", NO_UNITS_IN_TIER = "VGNUT", UNIT_NOT_FOUND = "VGUNF", TIER_NOT_FOUND = "VGTNF", } /** Checks if the error is a database error. */ export function isPostgresError(err: unknown): err is DatabaseError { return err instanceof DatabaseError } /** Sends a message detailing the given error on the given command context. */ export async function sendErrorMessage(ctx: CommandContext, err: unknown): Promise { console.log(err) if (isPostgresError(err)) { switch (err.code) { case ErrorCodes.BAD_CHANNEL_ADMIN: case ErrorCodes.BAD_CHANNEL_GAME: case ErrorCodes.BAD_GUILD_ADMIN: case ErrorCodes.BAD_GUILD_GAME: case ErrorCodes.NOT_YET_JOINED: case ErrorCodes.NOT_ENOUGH_CURRENCY: case ErrorCodes.DAILY_ALREADY_USED: case ErrorCodes.NO_UNITS_IN_TIER: case ErrorCodes.UNIT_NOT_FOUND: case ErrorCodes.TIER_NOT_FOUND: await ctx.send({ content: `**${err.message}**\n${err.detail}\n\n**Tip**: ${err.hint}`, ephemeral: true, }) return default: await ctx.send({ content: `**Unexpected Error${err.code ? ` (${err.code})` : ""}**${err.message ? `: ${err.message}` : ""}${err.detail ? `\n${err.detail}` : ""}${err.hint ? `\n\n**Tip**: ${err.hint}` : ""}`, ephemeral: true, }) return } } else { await ctx.send({ content: `**Unknown Error**: ${err}`, ephemeral: true, }) return } }