Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Read retry status from the SDK error, not the message

Rafi39f87c9b94fe
diff --git a/apps/web/lib/api/partners/search/providers/resilience.ts b/apps/web/lib/api/partners/search/providers/resilience.ts
index 489d2d388c..bce83022ce 100644
--- a/apps/web/lib/api/partners/search/providers/resilience.ts
+++ b/apps/web/lib/api/partners/search/providers/resilience.ts
@@ -1,6 +1,23 @@
+import {
+ APIConnectionError,
+ APIConnectionTimeoutError,
+ APIError,
+} from "@turbopuffer/turbopuffer";
+
const TRANSIENT_RETRY_ATTEMPTS = 2;
function isTransientError(error: unknown): boolean {
+ // The SDK's errors carry the status code, so it is read rather than sniffed
+ // out of the message, where a stray "500" in an ID or a count would
+ // misclassify a permanent failure as transient.
+ if (error instanceof APIError) {
+ return (
+ error instanceof APIConnectionError ||
+ error.status === 429 ||
+ (typeof error.status === "number" && error.status >= 500)
+ );
+ }
+
const message = error instanceof Error ? error.message : String(error);
// fetch network failures surface as TypeError ("fetch failed"), but so do
@@ -14,12 +31,16 @@ function isTransientError(error: unknown): boolean {
);
}
- return /\b(429|500|502|503|504)\b|rate.?limit|timeout|timed out|fetch failed|network|ECONNRESET|ETIMEDOUT/i.test(
+ return /timeout|timed out|fetch failed|network|ECONNRESET|ETIMEDOUT/i.test(
message,
);
}
function isTimeoutError(error: unknown): boolean {
+ if (error instanceof APIConnectionTimeoutError) {
+ return true;
+ }
+
const message = error instanceof Error ? error.message : String(error);
return /timeout|timed out|ETIMEDOUT/i.test(message);
}
diff --git a/apps/web/tests/partners/partner-search-resilience.test.ts b/apps/web/tests/partners/partner-search-resilience.test.ts
new file mode 100644
index 0000000000..a76739d17b
--- /dev/null
+++ b/apps/web/tests/partners/partner-search-resilience.test.ts
@@ -0,0 +1,37 @@
+import { withTransientRetry } from "@/lib/api/partners/search/providers/resilience";
+import { APIError } from "@turbopuffer/turbopuffer";
+import { describe, expect, it, vi } from "vitest";
+
+const apiError = (status: number) =>
+ new APIError(status, {}, `status ${status}`, new Headers());
+
+const failingOnce = (error: unknown) =>
+ vi
+ .fn()
+ .mockRejectedValueOnce(error)
+ .mockResolvedValueOnce("ok");
+
+describe("withTransientRetry", () => {
+ it.each([
+ ["a rate limit", apiError(429)],
+ ["a server error", apiError(503)],
+ ["a network failure", new Error("fetch failed")],
+ ])("retries %s", async (_label, error) => {
+ const operation = failingOnce(error);
+
+ await expect(withTransientRetry(operation)).resolves.toBe("ok");
+ expect(operation).toHaveBeenCalledTimes(2);
+ });
+
+ it.each([
+ // The status comes from the typed error, not the message, so digits in an
+ // ordinary message cannot make a permanent failure look transient.
+ ["a message that merely contains 500", new Error("wrote 500 documents")],
+ ["a client error", apiError(400)],
+ ])("does not retry %s", async (_label, error) => {
+ const operation = vi.fn().mockRejectedValue(error);
+
+ await expect(withTransientRetry(operation)).rejects.toBe(error);
+ expect(operation).toHaveBeenCalledTimes(1);
+ });
+});