From cc905e2051be4a840b3fd0c150b3888dc83e0aa9 Mon Sep 17 00:00:00 2001 From: Tim Wilson Date: Tue, 4 Feb 2025 14:26:10 -0500 Subject: [PATCH] Handle Dyandot DNS configuration errors --- apps/web/lib/dynadot/configure-dns.ts | 41 ++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/apps/web/lib/dynadot/configure-dns.ts b/apps/web/lib/dynadot/configure-dns.ts index e6f48b7645..bd9b5fa76c 100644 --- a/apps/web/lib/dynadot/configure-dns.ts +++ b/apps/web/lib/dynadot/configure-dns.ts @@ -1,5 +1,15 @@ +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({ + ResponseCode: z.string().or(z.number()), + Status: z.string(), + Error: z.string().optional(), + }), +}); + export const configureDNS = async ({ domain }: { domain: string }) => { const searchParams = new URLSearchParams({ key: DYNADOT_API_KEY, @@ -9,9 +19,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; };