Skip to content

Commit

Permalink
chore: show human-readable error when something fails at newsletter subs
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Sep 14, 2023
1 parent fef5c40 commit b1e3150
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
16 changes: 10 additions & 6 deletions src/back/routes/newsletter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NewsletterSubscriptionResult> {
const email = req.body.email
if (!isEmail(email)) {
throw new RequestError('Invalid email', RequestError.BadRequest)
Expand All @@ -28,13 +29,16 @@ async function handleSubscription(req: Request) {
},
body: `{ "email": "${email}" }`,
})

const data = await response.json()

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: '' }
}
5 changes: 5 additions & 0 deletions src/back/types/newsletter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type NewsletterSubscriptionResult = {
email: string
error: boolean
details: string | null
}
3 changes: 2 additions & 1 deletion src/clients/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -651,7 +652,7 @@ export class Governance extends API {
}

async subscribeToNewsletter(email: string) {
const response = await this.fetch<ApiResponse<string>>(
const response = await this.fetch<ApiResponse<NewsletterSubscriptionResult>>(
`/newsletter-subscribe`,
this.options().method('POST').json({
email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -20,34 +21,11 @@ import '../ProposalModal.css'

import './NewsletterSubscriptionModal.css'

type NewsletterSubscriptionResult = {
email: string
error: boolean
details: string | null
}

type Props = Omit<ModalProps, 'children'> & {
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()
Expand Down Expand Up @@ -89,7 +67,7 @@ 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)
if (subscriptionResult.error) {
setState({
isValid: false,
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal/ProposalModal.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}

.ProposalModal__Actions {
margin-top: 8px;
width: 100%;
}

Expand Down

0 comments on commit b1e3150

Please sign in to comment.