feat: improve logging

This commit is contained in:
eric
2026-02-23 13:32:13 +01:00
parent b9036ef962
commit 66b1ef15af
4 changed files with 134 additions and 13 deletions

View File

@@ -436,7 +436,13 @@ The image URL will appear in your response for the user to see.`;
* Handle errors gracefully
*/
async handleError(message: Message<true>, error: unknown): Promise<void> {
const err = error as Error & { status?: number };
const err = error as Error & {
status?: number;
code?: string;
type?: string;
error?: { message?: string; code?: string | number; metadata?: unknown };
request_id?: string;
};
if (err.status === 503) {
await message.reply(
@@ -445,6 +451,13 @@ The image URL will appear in your response for the user to see.`;
return;
}
if (err.status === 429) {
await message.reply(
"Jag pratar för mycket, ge mig en sekund... (rate limited)"
);
return;
}
if (err.name === "TimeoutError" || err.message?.includes("timeout")) {
await message.reply(
`Jag tog för lång tid på mig... (timeout)\n\`\`\`\n${err.message}\`\`\``
@@ -452,8 +465,20 @@ The image URL will appear in your response for the user to see.`;
return;
}
// Build a detailed error summary for the Discord reply
const errorParts: string[] = [];
if (err.status) errorParts.push(`HTTP ${err.status}`);
if (err.code) errorParts.push(`code: ${err.code}`);
if (err.type) errorParts.push(`type: ${err.type}`);
if (err.error?.message) errorParts.push(`provider: ${err.error.message}`);
if (err.request_id) errorParts.push(`req: ${err.request_id}`);
const summary = errorParts.length > 0
? errorParts.join(" | ")
: err.message || "Unknown error";
await message.reply({
content: `Något gick fel...\n\`\`\`\n${err.stack || err.message || JSON.stringify(error)}\`\`\``,
content: `Något gick fel...\n\`\`\`\n${summary}\`\`\``,
});
},
};