refactor: centralize target errors and cache lookups

This commit is contained in:
Peter Steinberger
2026-01-17 04:28:19 +00:00
parent 3efc5e54fa
commit 5fcc9b3244
20 changed files with 185 additions and 68 deletions

View File

@@ -35,6 +35,7 @@ import {
} from "./setup-helpers.js";
import { collectDiscordStatusIssues } from "./status-issues/discord.js";
import type { ChannelPlugin } from "./types.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("discord");
@@ -253,7 +254,7 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = {
return "DISCORD_BOT_TOKEN can only be used for the default account.";
}
if (!input.useEnv && !input.token) {
return "Discord requires targetken (or --use-env).";
return "Discord requires token (or --use-env).";
}
return null;
},
@@ -314,7 +315,7 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Discord requires target <channelId|user:ID|channel:ID>"),
error: missingTargetError("Discord", "<channelId|user:ID|channel:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -25,6 +25,7 @@ import {
migrateBaseNameToDefaultAccount,
} from "./setup-helpers.js";
import type { ChannelPlugin } from "./types.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("imessage");
@@ -177,7 +178,7 @@ export const imessagePlugin: ChannelPlugin<ResolvedIMessageAccount> = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to iMessage requires target <handle|chat_id:ID>"),
error: missingTargetError("iMessage", "<handle|chat_id:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -1,5 +1,6 @@
import { sendMessageDiscord, sendPollDiscord } from "../../../discord/send.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
export const discordOutbound: ChannelOutboundAdapter = {
deliveryMode: "direct",
@@ -11,7 +12,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Discord requires target <channelId|user:ID|channel:ID>"),
error: missingTargetError("Discord", "<channelId|user:ID|channel:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -2,6 +2,7 @@ import { chunkText } from "../../../auto-reply/chunk.js";
import { sendMessageIMessage } from "../../../imessage/send.js";
import { resolveChannelMediaMaxBytes } from "../media-limits.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
export const imessageOutbound: ChannelOutboundAdapter = {
deliveryMode: "direct",
@@ -12,7 +13,7 @@ export const imessageOutbound: ChannelOutboundAdapter = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to iMessage requires target <handle|chat_id:ID>"),
error: missingTargetError("iMessage", "<handle|chat_id:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -2,6 +2,7 @@ import { chunkText } from "../../../auto-reply/chunk.js";
import { sendMessageSignal } from "../../../signal/send.js";
import { resolveChannelMediaMaxBytes } from "../media-limits.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
export const signalOutbound: ChannelOutboundAdapter = {
deliveryMode: "direct",
@@ -12,9 +13,7 @@ export const signalOutbound: ChannelOutboundAdapter = {
if (!trimmed) {
return {
ok: false,
error: new Error(
"Delivering to Signal requires target <E.164|group:ID|signal:group:ID|signal:+E.164>",
),
error: missingTargetError("Signal", "<E.164|group:ID|signal:group:ID|signal:+E.164>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -1,5 +1,6 @@
import { sendMessageSlack } from "../../../slack/send.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
export const slackOutbound: ChannelOutboundAdapter = {
deliveryMode: "direct",
@@ -10,7 +11,7 @@ export const slackOutbound: ChannelOutboundAdapter = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Slack requires target <channelId|user:ID|channel:ID>"),
error: missingTargetError("Slack", "<channelId|user:ID|channel:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -1,6 +1,7 @@
import { markdownToTelegramHtmlChunks } from "../../../telegram/format.js";
import { sendMessageTelegram } from "../../../telegram/send.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
function parseReplyToMessageId(replyToId?: string | null) {
if (!replyToId) return undefined;
@@ -27,7 +28,7 @@ export const telegramOutbound: ChannelOutboundAdapter = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Telegram requires target <chatId>"),
error: missingTargetError("Telegram", "<chatId>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -3,6 +3,7 @@ import { shouldLogVerbose } from "../../../globals.js";
import { sendMessageWhatsApp, sendPollWhatsApp } from "../../../web/outbound.js";
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../../../whatsapp/normalize.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
export const whatsappOutbound: ChannelOutboundAdapter = {
deliveryMode: "gateway",
@@ -26,8 +27,9 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
}
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires target <E.164|group JID> or channels.whatsapp.allowFrom[0]",
error: missingTargetError(
"WhatsApp",
"<E.164|group JID> or channels.whatsapp.allowFrom[0]",
),
};
}
@@ -51,8 +53,9 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
}
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires target <E.164|group JID> or channels.whatsapp.allowFrom[0]",
error: missingTargetError(
"WhatsApp",
"<E.164|group JID> or channels.whatsapp.allowFrom[0]",
),
};
},

View File

@@ -26,6 +26,7 @@ import {
migrateBaseNameToDefaultAccount,
} from "./setup-helpers.js";
import type { ChannelPlugin } from "./types.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("signal");
@@ -201,8 +202,9 @@ export const signalPlugin: ChannelPlugin<ResolvedSignalAccount> = {
if (!trimmed) {
return {
ok: false,
error: new Error(
"Delivering to Signal requires target <E.164|group:ID|signal:group:ID|signal:+E.164>",
error: missingTargetError(
"Signal",
"<E.164|group:ID|signal:group:ID|signal:+E.164>",
),
};
}

View File

@@ -28,6 +28,7 @@ import {
migrateBaseNameToDefaultAccount,
} from "./setup-helpers.js";
import type { ChannelMessageActionName, ChannelPlugin } from "./types.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("slack");
@@ -497,7 +498,7 @@ export const slackPlugin: ChannelPlugin<ResolvedSlackAccount> = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Slack requires target <channelId|user:ID|channel:ID>"),
error: missingTargetError("Slack", "<channelId|user:ID|channel:ID>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -35,6 +35,7 @@ import {
} from "./setup-helpers.js";
import { collectTelegramStatusIssues } from "./status-issues/telegram.js";
import type { ChannelPlugin } from "./types.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("telegram");
@@ -215,7 +216,7 @@ export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = {
return "TELEGRAM_BOT_TOKEN can only be used for the default account.";
}
if (!input.useEnv && !input.token && !input.tokenFile) {
return "Telegram requires targetken or --token-file (or --use-env).";
return "Telegram requires token or --token-file (or --use-env).";
}
return null;
},
@@ -285,7 +286,7 @@ export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = {
if (!trimmed) {
return {
ok: false,
error: new Error("Delivering to Telegram requires target <chatId>"),
error: missingTargetError("Telegram", "<chatId>"),
};
}
return { ok: true, to: trimmed };

View File

@@ -35,6 +35,7 @@ import {
import { collectWhatsAppStatusIssues } from "./status-issues/whatsapp.js";
import type { ChannelMessageActionName, ChannelPlugin } from "./types.js";
import { resolveWhatsAppHeartbeatRecipients } from "./whatsapp-heartbeat.js";
import { missingTargetError } from "../../infra/outbound/target-errors.js";
const meta = getChatChannelMeta("whatsapp");
@@ -315,8 +316,9 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
}
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires target <E.164|group JID> or channels.whatsapp.allowFrom[0]",
error: missingTargetError(
"WhatsApp",
"<E.164|group JID> or channels.whatsapp.allowFrom[0]",
),
};
}
@@ -340,9 +342,7 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
}
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires target <E.164|group JID> or channels.whatsapp.allowFrom[0]",
),
error: missingTargetError("WhatsApp", "<E.164|group JID> or channels.whatsapp.allowFrom[0]"),
};
},
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {