From a8d9b0210a939d6e85a05f8a5fd5ad3755ce2037 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Tue, 19 Dec 2023 02:18:08 -0500 Subject: [PATCH] fix: catch error for invalid subcriber url on webhook ping test (#12784) Co-authored-by: CarinaWolli --- .../components/WebhookTestDisclosure.tsx | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/features/webhooks/components/WebhookTestDisclosure.tsx b/packages/features/webhooks/components/WebhookTestDisclosure.tsx index 145c038615..52db5aee4e 100644 --- a/packages/features/webhooks/components/WebhookTestDisclosure.tsx +++ b/packages/features/webhooks/components/WebhookTestDisclosure.tsx @@ -1,7 +1,9 @@ import { useWatch } from "react-hook-form"; +import { ZodError } from "zod"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; +import { ZTestTriggerInputSchema } from "@calcom/trpc/server/routers/viewer/webhook/testTrigger.schema"; import { Badge, Button, showToast } from "@calcom/ui"; import { Activity } from "@calcom/ui/components/icon"; @@ -27,9 +29,25 @@ export default function WebhookTestDisclosure() { color="secondary" disabled={mutation.isLoading || !subscriberUrl} StartIcon={Activity} - onClick={() => - mutation.mutate({ url: subscriberUrl, secret: webhookSecret, type: "PING", payloadTemplate }) - }> + onClick={() => { + try { + ZTestTriggerInputSchema.parse({ + url: subscriberUrl, + secret: webhookSecret, + type: "PING", + payloadTemplate, + }); + mutation.mutate({ url: subscriberUrl, secret: webhookSecret, type: "PING", payloadTemplate }); + } catch (error) { + //this catches invalid subscriberUrl before calling the mutation + if (error instanceof ZodError) { + const errorMessage = error.errors.map((e) => e.message).join(", "); + showToast(errorMessage, "error"); + } else { + showToast(t("unexpected_error_try_again"), "error"); + } + } + }}> {t("ping_test")}