Skip to content

Commit

Permalink
chore: better error handling (dont always ping me)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyko committed Nov 19, 2023
1 parent bb68111 commit 544573f
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 16 deletions.
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/adjective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import i18n from "i18next";
import { injectable } from "tsyringe";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

@injectable()
Expand All @@ -26,7 +27,7 @@ export default class extends Command<typeof AdjectiveCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/close-rhyme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof CloseRhymeCommand> {
Expand All @@ -27,7 +28,7 @@ export default class extends Command<typeof CloseRhymeCommand> {
args.word,
).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
5 changes: 3 additions & 2 deletions apps/bot/src/commands/general/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createPronunciationURL, fetchDefinition } from "#mw";
import { formatText } from "#mw/format.js";
import { RedisManager } from "#structures";
import { Characters, Emojis } from "#util/constants.js";
import { CommandError } from "#util/error.js";
import { kRedis, trimArray } from "#util/index.js";

@injectable()
Expand All @@ -51,9 +52,9 @@ export default class extends Command<typeof DefinitionCommand> {
// will be string[] if no defs were found and the api provided suggestions
// will be Entry[] if defs were found
if (!defRes.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
if (typeof defRes !== "object") {
throw new TypeError(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
}

if (typeof defRes[0] === "object") {
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/holonyms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof HolonymsCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof HolonymsCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/homophones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof HomophonesCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof HomophonesCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/hyponyms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof HyponymsCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof HyponymsCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/match-word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof MatchCommand> {
Expand All @@ -25,7 +26,7 @@ export default class extends Command<typeof MatchCommand> {
args.word,
).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/noun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof NounCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof NounCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/rhyme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof RhymeCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof RhymeCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/similar-meaning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuseRaw } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof SimilarMeaningCommand> {
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class extends Command<typeof SimilarMeaningCommand> {

const words = await fetchDatamuseRaw(search).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

const content = startsWith
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/similar-spelling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof SimilarSpellingCommand> {
Expand All @@ -25,7 +26,7 @@ export default class extends Command<typeof SimilarSpellingCommand> {
args.word,
).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/sounds-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof SoundsLikeCommand> {
Expand All @@ -25,7 +26,7 @@ export default class extends Command<typeof SoundsLikeCommand> {
args.word,
).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/that-follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuseRaw } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof ThatFollowCommand> {
Expand Down Expand Up @@ -37,7 +38,7 @@ export default class extends Command<typeof ThatFollowCommand> {

const words = await fetchDatamuseRaw(search).catch(() => null);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

const content = startsWith
Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/commands/general/triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import i18n from "i18next";
import { parseLimit } from "#util/args.js";
import { DatamuseQuery, fetchDatamuse } from "#util/datamuse.js";
import { CommandError } from "#util/error.js";
import { firstUpperCase, trimArray } from "#util/index.js";

export default class extends Command<typeof TriggersCommand> {
Expand All @@ -24,7 +25,7 @@ export default class extends Command<typeof TriggersCommand> {
() => null,
);
if (!words?.length)
throw new Error(i18n.t("common.errors.not_found", { lng }));
throw new CommandError(i18n.t("common.errors.not_found", { lng }));
const mapped = words.map((h) => h.word);

await interaction.editReply(
Expand Down
4 changes: 3 additions & 1 deletion apps/bot/src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Counter, Registry } from "prom-client";
import { container, inject, injectable } from "tsyringe";
import { logger } from "#logger";
import { RedisManager } from "#structures";
import { CommandError } from "#util/error.js";
import { fetchAutocomplete, fetchTopWords } from "#util/mw/index.js";
import { kRedis } from "#util/symbols.js";

Expand Down Expand Up @@ -163,11 +164,12 @@ export default class implements Event {
});
}
} catch (error) {
const isCommandError = error instanceof CommandError;
const err = error as Error;
logger.error(err, err.message);

void this.webhook.send({
content: `<@${process.env.OWNER_ID}>`,
content: isCommandError ? `<@${process.env.OWNER_ID}>` : "",
embeds: [
new EmbedBuilder()
.setTitle(interaction.commandName)
Expand Down
13 changes: 13 additions & 0 deletions apps/bot/src/util/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* An error that is thrown when a command fails.
*
* This doesn't need to be logged as an error, as it is a normal part of the
* command execution process.
*/
export class CommandError extends Error {
public constructor(message: string) {
super(message);

this.name = "CommandError";
}
}

0 comments on commit 544573f

Please sign in to comment.