improve errors

This commit is contained in:
artie 2025-03-27 17:43:40 +01:00
parent a59dd8a32a
commit 1568439a42
5 changed files with 50 additions and 17 deletions

View File

@ -5,7 +5,9 @@ import {
Collection, Collection,
GatewayIntentBits, GatewayIntentBits,
InteractionContextType, InteractionContextType,
lazy,
Partials, Partials,
TextChannel,
} from "discord.js"; } from "discord.js";
import { REST } from "@discordjs/rest"; import { REST } from "@discordjs/rest";
import { env } from "./env"; import { env } from "./env";
@ -56,6 +58,19 @@ export class ArtemisClient extends Client {
return this.users.fetch(this.ownerId); return this.users.fetch(this.ownerId);
} }
getDevWebhook = lazy(async () => {
const channel = (await client.channels.fetch(
env.DEV_CHANNEL_ID
)) as TextChannel;
return channel
.fetchWebhooks()
.then(
webhooks =>
webhooks.find(wh => wh.applicationId === env.APPLICATION_ID) ??
channel.createWebhook({ name: "artemis" })
);
});
async loadCommands() { async loadCommands() {
const commandsDir = path.join(import.meta.dir, "commands"); const commandsDir = path.join(import.meta.dir, "commands");
const categories = await fs const categories = await fs

View File

@ -52,7 +52,7 @@ export async function translateImpl(
target target
).catch(err => { ).catch(err => {
logger.error(err, "DeepL error, falling back to Google Translate"); logger.error(err, "DeepL error, falling back to Google Translate");
sendErrorAlert(err); sendErrorAlert(err, { source, target, ocrModel });
return translateGoogle(text, "auto", "en"); return translateGoogle(text, "auto", "en");
}); });

View File

@ -88,7 +88,7 @@ export async function ocrImpl(url: string) {
const result = await lensOcr(compressed) const result = await lensOcr(compressed)
.catch(err => { .catch(err => {
logger.error(err, "Google Lens error, falling back to Yandex"); logger.error(err, "Google Lens error, falling back to Yandex");
sendErrorAlert(err); sendErrorAlert(err, { mime: type.mime });
return yandexOcr(compressed, type.mime); return yandexOcr(compressed, type.mime);
}) })
.catch(() => abort("Failed to OCR the image")); .catch(() => abort("Failed to OCR the image"));

View File

@ -75,7 +75,11 @@ async function handleChatInputCommand(
const trace = nanoid(); const trace = nanoid();
content += `\ntrace: ${inlineCode(trace)}`; content += `\ntrace: ${inlineCode(trace)}`;
logger.error({ trace, err }); logger.error({ trace, err });
sendErrorAlert(err, trace); sendErrorAlert(err, {
trace,
command: command.data.name,
user: `${interaction.user.id} (${interaction.user.tag})`,
});
} }
await interaction[ await interaction[

View File

@ -1,4 +1,4 @@
import { codeBlock } from "discord.js"; import { bold, codeBlock } from "discord.js";
import { client } from "../client"; import { client } from "../client";
export class ExplicitCommandError extends Error {} export class ExplicitCommandError extends Error {}
@ -13,17 +13,31 @@ export function isExplicitCommandError(
return error instanceof ExplicitCommandError; return error instanceof ExplicitCommandError;
} }
export async function sendErrorAlert(error: any, trace?: string) { export async function sendErrorAlert(
return client.getOwner().then(owner => error: any,
owner.send({ meta?: Record<string, string | null | undefined>
content: trace, ) {
embeds: [ const owner = await client.getOwner();
{ const webhook = await client.getDevWebhook();
title: "Unhandled Error",
description: codeBlock("js", error.stack ?? error.message), return webhook.send({
color: 0xff0000, username: "artemis error",
}, avatarURL: "https://files.catbox.moe/g52ano.png",
], allowedMentions: { users: [owner.id] },
}) content:
); `${owner}\n` +
(meta
? Object.entries(meta)
.filter(([_, value]) => value)
.map(([key, value]) => `${bold(key)}: ${value}`)
.join("\n")
: ""),
embeds: [
{
title: "Unhandled Error",
description: codeBlock("js", error.stack ?? error.message),
color: 0xff0000,
},
],
});
} }