error handling

This commit is contained in:
artie 2025-02-08 16:46:52 +01:00
parent cb4d7df241
commit 5fa93ca95e
5 changed files with 43 additions and 13 deletions

View File

@ -1,10 +1,10 @@
import eslint from "@eslint/js"; import eslint from "@eslint/js";
import tseslint from "typescript-eslint"; import tseslint from "typescript-eslint";
export default [ export default tseslint.config(
eslint.configs.recommended, eslint.configs.recommended,
...tseslint.configs.recommended, tseslint.configs.recommended,
...tseslint.configs.strict, tseslint.configs.strict,
{ {
rules: { rules: {
"no-empty": ["error", { allowEmptyCatch: true }], "no-empty": ["error", { allowEmptyCatch: true }],
@ -15,8 +15,5 @@ export default [
"@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-explicit-any": "off",
}, },
}, }
{ );
ignores: ["dist/"],
},
];

View File

@ -32,6 +32,10 @@ export class ArtemisClient extends Client {
}); });
this.api = api; this.api = api;
this.on("error", (err) => {
log.error("Unhandled Client Error", err);
});
} }
async setup() { async setup() {

View File

@ -7,6 +7,7 @@ import {
import { client } from "../client"; import { client } from "../client";
import { log } from "../utils/logger"; import { log } from "../utils/logger";
import { defineEvent } from "."; import { defineEvent } from ".";
import { isCommandError, isError } from "../utils/error";
const running = new Map<string, number>(); const running = new Map<string, number>();
const getRunning = (command: string) => running.get(command) ?? 0; const getRunning = (command: string) => running.get(command) ?? 0;
@ -50,12 +51,19 @@ async function handleChatInputCommand(
try { try {
await command.execute(interaction); await command.execute(interaction);
} catch (error) { } catch (err) {
log.error(error); const content = isCommandError(err)
? err.message
: isError(err)
? err.message
: "An unknown error occurred!";
if (!isCommandError(err)) log.error("Unhandled Command Error", err);
await interaction[ await interaction[
interaction.replied || interaction.deferred ? "followUp" : "reply" interaction.replied || interaction.deferred ? "followUp" : "reply"
]({ ]({
content: "There was an error while executing this command!", content,
}); });
} finally { } finally {
if (command.maxConcurrency) { if (command.maxConcurrency) {
@ -74,8 +82,8 @@ async function handleAutocomplete(interaction: AutocompleteInteraction) {
if (command.autocomplete) { if (command.autocomplete) {
try { try {
await command.autocomplete(interaction); await command.autocomplete(interaction);
} catch (error) { } catch (err) {
log.error(error); log.error("Autocomplete Error", err);
} }
} }
} }

View File

@ -1,5 +1,17 @@
import { env } from "./env"; import { env } from "./env";
import { client } from "./client"; import { client } from "./client";
import { log } from "./utils/logger";
import { DiscordAPIError } from "discord.js";
process.on("unhandledRejection", (err) => {
if (err instanceof DiscordAPIError && err.status >= 500) return;
log.error("Unhandled Rejection", err);
});
process.on("uncaughtException", (err) => {
log.error("Uncaught Exception, restarting...", err);
process.exit(1);
});
await client.setup(); await client.setup();
client.login(env.DISCORD_TOKEN); client.login(env.DISCORD_TOKEN);

9
src/utils/error.ts Normal file
View File

@ -0,0 +1,9 @@
export class CommandError extends Error {}
export function isCommandError(error: any): error is CommandError {
return error instanceof CommandError;
}
export function isError(error: any): error is Error {
return error instanceof Error;
}