From 9db468bd96ebcbd568f54c94cb367aa8f1daa7ce Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Thu, 2 Nov 2023 11:45:36 +0000 Subject: [PATCH] fix: errors from zendesk api (#18345) --- .../lib/components/Support/supportLogic.ts | 76 ++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/frontend/src/lib/components/Support/supportLogic.ts b/frontend/src/lib/components/Support/supportLogic.ts index a0e5c0a4ff480..266ead19094db 100644 --- a/frontend/src/lib/components/Support/supportLogic.ts +++ b/frontend/src/lib/components/Support/supportLogic.ts @@ -258,43 +258,51 @@ export const supportLogic = kea([ }, }, } - await fetch('https://posthoghelp.zendesk.com/api/v2/requests.json', { - method: 'POST', - body: JSON.stringify(payload, undefined, 4), - headers: { 'Content-Type': 'application/json' }, - }) - .then((res) => res.json()) - .then((res) => { - const zendesk_ticket_id = res.request.id - const zendesk_ticket_link = `https://posthoghelp.zendesk.com/agent/tickets/${zendesk_ticket_id}` - const properties = { - zendesk_ticket_uuid, - kind, - target_area, - message, - zendesk_ticket_id, - zendesk_ticket_link, - } - posthog.capture('support_ticket', properties) - Sentry.captureMessage('User submitted Zendesk ticket', { - tags: { - zendesk_ticket_uuid, - zendesk_ticket_link, - support_request_kind: kind, - support_request_area: target_area, - team_id: teamLogic.values.currentTeamId, - }, - extra: properties, - level: 'log', - }) - lemonToast.success( - "Got the message! If we have follow-up information for you, we'll reply via email." - ) + + try { + const response = await fetch('https://posthoghelp.zendesk.com/api/v2/requests.json', { + method: 'POST', + body: JSON.stringify(payload, undefined, 4), + headers: { 'Content-Type': 'application/json' }, }) - .catch((err) => { - captureException(err) + if (!response.ok) { + const error = new Error(`There was an error creating the support ticket with zendesk.`) + captureException(error, { + extra: { response, payload }, + }) lemonToast.error(`There was an error sending the message.`) + return + } + + const json = await response.json() + + const zendesk_ticket_id = json.request.id + const zendesk_ticket_link = `https://posthoghelp.zendesk.com/agent/tickets/${zendesk_ticket_id}` + const properties = { + zendesk_ticket_uuid, + kind, + target_area, + message, + zendesk_ticket_id, + zendesk_ticket_link, + } + posthog.capture('support_ticket', properties) + Sentry.captureMessage('User submitted Zendesk ticket', { + tags: { + zendesk_ticket_uuid, + zendesk_ticket_link, + support_request_kind: kind, + support_request_area: target_area, + team_id: teamLogic.values.currentTeamId, + }, + extra: properties, + level: 'log', }) + lemonToast.success("Got the message! If we have follow-up information for you, we'll reply via email.") + } catch (e) { + captureException(e) + lemonToast.error(`There was an error sending the message.`) + } }, closeSupportForm: () => {