Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: classify Slack unfurl failures

Richie McIlroyd439e7ee999d
diff --git a/apps/web/__tests__/unit/slack-client.test.ts b/apps/web/__tests__/unit/slack-client.test.ts
index 6adfe71ae..32c6fe948 100644
--- a/apps/web/__tests__/unit/slack-client.test.ts
+++ b/apps/web/__tests__/unit/slack-client.test.ts
@@ -3,6 +3,7 @@ import {
buildSlackInstallUrl,
exchangeSlackOAuthCode,
SLACK_BOT_SCOPES,
+ type SlackUnfurlError,
sendSlackUnfurl,
} from "@/lib/slack/client";
@@ -124,4 +125,66 @@ describe("Slack API client", () => {
source: "conversations_history",
});
});
+
+ it("marks exhausted transient failures for durable recovery", async () => {
+ const fetchImpl = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ ok: false, error: "server_error" }), {
+ status: 503,
+ headers: { "Content-Type": "application/json" },
+ }),
+ );
+
+ await expect(
+ sendSlackUnfurl({
+ token: "xoxb-token",
+ event: {
+ channel: "C123",
+ messageTs: "123.456",
+ },
+ unfurls: {
+ "https://cap.so/s/abc123": {
+ blocks: [{ type: "video" }],
+ },
+ },
+ fetchImpl: fetchImpl as unknown as typeof fetch,
+ sleepImpl: vi.fn(async () => undefined),
+ }),
+ ).rejects.toMatchObject({
+ name: "SlackUnfurlError",
+ retryable: true,
+ } satisfies Partial<SlackUnfurlError>);
+ expect(fetchImpl).toHaveBeenCalledTimes(3);
+ });
+
+ it("marks permanent Slack failures as non-retryable", async () => {
+ const fetchImpl = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ ok: false, error: "invalid_auth" }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ }),
+ );
+
+ await expect(
+ sendSlackUnfurl({
+ token: "xoxb-token",
+ event: {
+ channel: "C123",
+ messageTs: "123.456",
+ },
+ unfurls: {
+ "https://cap.so/s/abc123": {
+ blocks: [{ type: "video" }],
+ },
+ },
+ fetchImpl: fetchImpl as unknown as typeof fetch,
+ sleepImpl: vi.fn(async () => undefined),
+ }),
+ ).rejects.toMatchObject({
+ name: "SlackUnfurlError",
+ retryable: false,
+ } satisfies Partial<SlackUnfurlError>);
+ expect(fetchImpl).toHaveBeenCalledOnce();
+ });
});
diff --git a/apps/web/lib/slack/client.ts b/apps/web/lib/slack/client.ts
index bcda56492..cf4255ab7 100644
--- a/apps/web/lib/slack/client.ts
+++ b/apps/web/lib/slack/client.ts
@@ -27,6 +27,16 @@ type SlackApiResponse = {
error?: string;
};
+export class SlackUnfurlError extends Error {
+ readonly retryable: boolean;
+
+ constructor(message: string, retryable: boolean) {
+ super(message);
+ this.name = "SlackUnfurlError";
+ this.retryable = retryable;
+ }
+}
+
const sleep = (durationMs: number) =>
new Promise<void>((resolve) => setTimeout(resolve, durationMs));
@@ -191,8 +201,9 @@ export const sendSlackUnfurl = async ({
const retryable = response.status === 429 || response.status >= 500;
if (!retryable || attempt === 3) {
- throw new Error(
+ throw new SlackUnfurlError(
`Slack unfurl failed: ${typeof body.error === "string" ? body.error : response.status}`,
+ retryable,
);
}
const retryAfterSeconds = Number(response.headers.get("Retry-After"));
@@ -202,10 +213,7 @@ export const sendSlackUnfurl = async ({
: attempt * 250;
await sleepImpl(Math.min(retryAfterMs, 2000));
} catch (error) {
- if (
- attempt === 3 ||
- (error instanceof Error && error.message.startsWith("Slack unfurl"))
- ) {
+ if (attempt === 3 || error instanceof SlackUnfurlError) {
throw error;
}
await sleepImpl(attempt * 250);