Skip to content

Commit

Permalink
Merge pull request #1975 from dubinc/configure-dns-error-handling
Browse files Browse the repository at this point in the history
Improve DNS configuration error handling
  • Loading branch information
steven-tey authored Feb 4, 2025
2 parents b5a2f92 + 99ba4bb commit b0ab730
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions apps/web/lib/dynadot/configure-dns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { z } from "zod";
import { DubApiError } from "../api/errors";
import { DYNADOT_API_KEY, DYNADOT_BASE_URL } from "./constants";

const schema = z.object({
SetDnsResponse: z.object({
Status: z.string(),
Error: z.string().optional(),
}),
});

export const configureDNS = async ({ domain }: { domain: string }) => {
const searchParams = new URLSearchParams({
key: DYNADOT_API_KEY,
Expand All @@ -9,9 +18,32 @@ export const configureDNS = async ({ domain }: { domain: string }) => {
main_record0: "76.76.21.21",
});

return fetch(`${DYNADOT_BASE_URL}?${searchParams.toString()}`, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
const response = await fetch(
`${DYNADOT_BASE_URL}?${searchParams.toString()}`,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
},
}).then((res) => res.json());
);

if (!response.ok) {
throw new DubApiError({
code: "bad_request",
message: `Failed to configure DNS for domain "${domain}": ${response.statusText}`,
});
}

const data = schema.parse(await response.json());

const { Status, Error } = data.SetDnsResponse;

if (Status !== "success") {
throw new DubApiError({
code: "bad_request",
message: `Failed to configure DNS for domain "${domain}": ${Error}`,
});
}

return data;
};

0 comments on commit b0ab730

Please sign in to comment.