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.

Retry Dynadot domain search once on a network error or timeout

Rafidb1ff4476642
diff --git a/apps/web/lib/dynadot/client.ts b/apps/web/lib/dynadot/client.ts
index 265b67a60f..36d423c286 100644
--- a/apps/web/lib/dynadot/client.ts
+++ b/apps/web/lib/dynadot/client.ts
@@ -45,11 +45,15 @@ class DynadotClient extends HttpBaseClient {
}
// GET /api3.json?command=search
- async search(input: z.input<typeof searchDomainsInputSchema>) {
+ async search(
+ input: z.input<typeof searchDomainsInputSchema>,
+ { timeout }: { timeout?: number } = {},
+ ) {
return await this.get("", {
input,
inputSchema: searchDomainsInputSchema,
outputSchema: searchDomainsOutputSchema,
+ timeout,
});
}
diff --git a/apps/web/lib/dynadot/search-domains.ts b/apps/web/lib/dynadot/search-domains.ts
index e51466e69d..09b6b24d4f 100644
--- a/apps/web/lib/dynadot/search-domains.ts
+++ b/apps/web/lib/dynadot/search-domains.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "../api/errors";
+import { HttpClientError } from "../http/base-client";
import { DomainStatusSchema } from "../zod/schemas/domains";
import {
parseRegistrationPriceUsdCents,
@@ -6,12 +7,15 @@ import {
} from "./calculate-domain-price";
import { dynadotClient } from "./client";
+const SEARCH_TIMEOUT_MS = 10_000;
+const SEARCH_ATTEMPTS = 2;
+
export const searchDomainsAvailability = async ({
domains,
}: {
domains: Record<string, string>;
}) => {
- const data = await dynadotClient.search({
+ const data = await searchWithRetry({
...domains,
command: "search",
show_price: "1",
@@ -51,3 +55,34 @@ export const searchDomainsAvailability = async ({
return DomainStatusSchema.array().parse(result);
};
+
+// The user waits for the search result, so retry a network error or timeout once
+// with a short timeout, then return an error message the UI can show.
+async function searchWithRetry(
+ input: Parameters<typeof dynadotClient.search>[0],
+) {
+ for (let attempt = 1; ; attempt++) {
+ try {
+ return await dynadotClient.search(input, {
+ timeout: SEARCH_TIMEOUT_MS,
+ });
+ } catch (error) {
+ // A null status means Dynadot sent no response (network error or timeout).
+ // Request validation errors also have a null status, but the route validates the domain first.
+ const noResponse =
+ error instanceof HttpClientError && error.status === null;
+
+ if (!noResponse) {
+ throw error;
+ }
+
+ if (attempt >= SEARCH_ATTEMPTS) {
+ throw new DubApiError({
+ code: "internal_server_error",
+ message:
+ "Domain search is unavailable right now. Please try again in a few minutes.",
+ });
+ }
+ }
+ }
+}