From 97609d2a12068bf85431a04b03bf9dc3121bd722 Mon Sep 17 00:00:00 2001 From: 1emu Date: Thu, 14 Sep 2023 14:19:47 -0300 Subject: [PATCH] chore: show human-readable error when something fails at newsletter subs --- src/back/routes/newsletter.ts | 18 ++++++++----- src/back/types/newsletter.ts | 5 ++++ src/clients/Governance.ts | 3 ++- .../NewsletterSubscriptionModal.tsx | 27 +++---------------- src/components/Modal/ProposalModal.css | 1 + 5 files changed, 23 insertions(+), 31 deletions(-) create mode 100644 src/back/types/newsletter.ts diff --git a/src/back/routes/newsletter.ts b/src/back/routes/newsletter.ts index 8bb7e876f..35e7e86d5 100644 --- a/src/back/routes/newsletter.ts +++ b/src/back/routes/newsletter.ts @@ -7,12 +7,13 @@ import isEmail from 'validator/lib/isEmail' import { ErrorService } from '../../services/ErrorService' import { ErrorCategory } from '../../utils/errorCategories' +import { NewsletterSubscriptionResult } from '../types/newsletter' export default routes((router) => { router.post('/newsletter-subscribe', handleAPI(handleSubscription)) }) -async function handleSubscription(req: Request) { +async function handleSubscription(req: Request): Promise { const email = req.body.email if (!isEmail(email)) { throw new RequestError('Invalid email', RequestError.BadRequest) @@ -28,13 +29,18 @@ async function handleSubscription(req: Request) { }, body: `{ "email": "${email}" }`, }) - const data = await response.json() + + console.log('data', data) + if (data.errors) { - const errorMessage = 'Error subscribing to newsletter' - ErrorService.report(errorMessage, { email, error: JSON.stringify(data.errors), category: ErrorCategory.Newsletter }) - throw new RequestError(errorMessage, RequestError.InternalServerError) + ErrorService.report('Error subscribing to newsletter', { + email, + error: JSON.stringify(data.errors), + category: ErrorCategory.Newsletter, + }) + return { email, error: true, details: 'Invalid email' } } - return data + return { email, error: false, details: '' } } diff --git a/src/back/types/newsletter.ts b/src/back/types/newsletter.ts new file mode 100644 index 000000000..1414dadc2 --- /dev/null +++ b/src/back/types/newsletter.ts @@ -0,0 +1,5 @@ +export type NewsletterSubscriptionResult = { + email: string + error: boolean + details: string | null +} diff --git a/src/clients/Governance.ts b/src/clients/Governance.ts index d20f0aef8..688db66f5 100644 --- a/src/clients/Governance.ts +++ b/src/clients/Governance.ts @@ -4,6 +4,7 @@ import env from 'decentraland-gatsby/dist/utils/env' import snakeCase from 'lodash/snakeCase' import { AirdropOutcome } from '../back/types/AirdropJob' +import { NewsletterSubscriptionResult } from '../back/types/newsletter' import { SpecState } from '../components/Debug/UploadBadgeSpec' import { GOVERNANCE_API } from '../constants' import { BadgeCreationResult, RevokeOrReinstateResult, UserBadges } from '../entities/Badges/types' @@ -651,7 +652,7 @@ export class Governance extends API { } async subscribeToNewsletter(email: string) { - const response = await this.fetch>( + const response = await this.fetch>( `/newsletter-subscribe`, this.options().method('POST').json({ email, diff --git a/src/components/Modal/NewsletterSubscriptionModal/NewsletterSubscriptionModal.tsx b/src/components/Modal/NewsletterSubscriptionModal/NewsletterSubscriptionModal.tsx index 70c66806c..ab9d7af8c 100644 --- a/src/components/Modal/NewsletterSubscriptionModal/NewsletterSubscriptionModal.tsx +++ b/src/components/Modal/NewsletterSubscriptionModal/NewsletterSubscriptionModal.tsx @@ -9,6 +9,7 @@ import { Header } from 'decentraland-ui/dist/components/Header/Header' import { Modal, ModalProps } from 'decentraland-ui/dist/components/Modal/Modal' import isEmail from 'validator/lib/isEmail' +import { NewsletterSubscriptionResult } from '../../../back/types/newsletter' import { Governance } from '../../../clients/Governance' import useAsyncTask from '../../../hooks/useAsyncTask' import useFormatMessage from '../../../hooks/useFormatMessage' @@ -20,34 +21,11 @@ import '../ProposalModal.css' import './NewsletterSubscriptionModal.css' -type NewsletterSubscriptionResult = { - email: string - error: boolean - details: string | null -} - type Props = Omit & { onSubscriptionSuccess?: () => void subscribed: boolean } -async function subscribe(email: string) { - try { - await Governance.get().subscribeToNewsletter(email) - return { - email, - error: false, - details: null, - } - } catch (err) { - return { - email, - error: true, - details: (err as any).body.detail, - } - } -} - export function NewsletterSubscriptionModal({ onSubscriptionSuccess, subscribed, ...props }: Props) { const [account] = useAuthContext() const t = useFormatMessage() @@ -89,7 +67,8 @@ export function NewsletterSubscriptionModal({ onSubscriptionSuccess, subscribed, const [subscribing, handleAccept] = useAsyncTask(async () => { if (state.email && isEmail(state.email) && onSubscriptionSuccess) { - const subscriptionResult: NewsletterSubscriptionResult = await subscribe(state.email) + const subscriptionResult: NewsletterSubscriptionResult = await Governance.get().subscribeToNewsletter(state.email) + console.log('subscriptionResult', subscriptionResult) if (subscriptionResult.error) { setState({ isValid: false, diff --git a/src/components/Modal/ProposalModal.css b/src/components/Modal/ProposalModal.css index 54dfab3f7..8d9f6e048 100644 --- a/src/components/Modal/ProposalModal.css +++ b/src/components/Modal/ProposalModal.css @@ -26,6 +26,7 @@ } .ProposalModal__Actions { + margin-top: 8px; width: 100%; }