Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat: add error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Apr 15, 2024
1 parent 4d249af commit 5c51c1c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
30 changes: 30 additions & 0 deletions apps/bot/src/lib/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ErrorHandler as IgloErrorHandler, InteractionHandlerError } from "@snowcrystals/iglo";
import { bold, underline } from "colorette";
import { DiscordAPIError, type Interaction } from "discord.js";

export class ErrorHandler extends IgloErrorHandler {
/**
* Logs the error and makes sure the user is aware of the situation. This function is overridable for customisability.
* @param error The error that was emitted
* @param interaction The interaction from the user that used the bot
*/
public override async handleError(error: Error, interaction?: Interaction): Promise<void> {
if (error instanceof InteractionHandlerError) {
let fatal = false;
if (["InvalidDirectory", "noConstructorOptions"].includes(error.type)) fatal = true;

const message = `${bold(underline(`InteractionHandlerError(${error.type})`))}: ${error.message}`;
this.client.logger[fatal ? "fatal" : "error"](message);
} else if (error instanceof DiscordAPIError && interaction && !this.isSilencedError(interaction.channelId ?? "", interaction.guildId, error))
this.client.logger.error(`${bold(underline(`DiscordAPIError(${error.name})`))}: ${error.message}`);
else this.client.logger.error(`${bold(underline(`Error(${error.name})`))}: ${error.message} --- raw=`, error);

if (interaction && interaction.isRepliable())
await interaction
.followUp({
content:
"Welcome to our corner of errors, a place you shouldn't come to too often. It is probably not your fault though, something on our side brought you here. Stay safe out there, if this happens again make sure to contact the support team."
})
.catch(() => void 0);
}
}
4 changes: 3 additions & 1 deletion apps/bot/src/lib/GitcordClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { IgloClient, LogLevel } from "@snowcrystals/iglo";
import { env } from "@/shared/env.js";

import { BOT_COMMANDS_DIR, BOT_LISTENER_DIR } from "./constants.js";
import { ErrorHandler } from "./ErrorHandler.js";

export default class GitCordClient extends IgloClient {
public constructor() {
super({
client: { intents: ["GuildWebhooks", "Guilds"], allowedMentions: { repliedUser: true, roles: [], users: [] } },
paths: { commands: BOT_COMMANDS_DIR, events: BOT_LISTENER_DIR },
logger: { level: process.env.NODE_ENV === "development" ? LogLevel.Debug : LogLevel.Info, parser: { depth: 2 } }
logger: { level: process.env.NODE_ENV === "development" ? LogLevel.Debug : LogLevel.Info, parser: { depth: 2 } },
errorHandler: ErrorHandler
});
}

Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
image: ghcr.io/ijskoud/gitcord:api
restart: always

volumes:
- ~/gitcord/.env:/gitcord/.env.local
env_file:
- ~/gitcord/.env
ports:
- 3001:3000

Expand All @@ -14,5 +14,5 @@ services:
image: ghcr.io/ijskoud/gitcord:bot
restart: always

volumes:
- ~/gitcord/.env:/gitcord/.env.local
env_file:
- ~/gitcord/.env

0 comments on commit 5c51c1c

Please sign in to comment.