You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
2 years ago
|
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",
|
||
|
}
|
||
|
|
||
|
/** 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<void> {
|
||
|
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:
|
||
|
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.message}\n${err.detail}\n\n**Tip**: ${err.hint}`,
|
||
|
ephemeral: true,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
await ctx.send({
|
||
|
content: `**Unknown Error**: ${err}`,
|
||
|
ephemeral: true,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
}
|