Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: show human-readable error when something fails at newsletter subs #1260

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/back/routes/newsletter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import fetch from 'isomorphic-fetch'
import isEmail from 'validator/lib/isEmail'

import { ErrorService } from '../../services/ErrorService'
import { NewsletterSubscriptionResult } from '../../shared/types/newsletter'
import { ErrorCategory } from '../../utils/errorCategories'

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: '' }
}
3 changes: 2 additions & 1 deletion src/clients/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SubscriptionAttributes } from '../entities/Subscription/types'
import { Topic } from '../entities/SurveyTopic/types'
import { ProjectHealth, UpdateAttributes, UpdateResponse } from '../entities/Updates/types'
import { Vote, VotedProposal, Voter } from '../entities/Votes/types'
import { NewsletterSubscriptionResult } from '../shared/types/newsletter'
import Time from '../utils/date/Time'

import { TransparencyBudget } from './DclData'
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 @@ -12,6 +12,7 @@ import isEmail from 'validator/lib/isEmail'
import { Governance } from '../../../clients/Governance'
import useAsyncTask from '../../../hooks/useAsyncTask'
import useFormatMessage from '../../../hooks/useFormatMessage'
import { NewsletterSubscriptionResult } from '../../../shared/types/newsletter'
import { ANONYMOUS_USR_SUBSCRIPTION, NEWSLETTER_SUBSCRIPTION_KEY } from '../../Banner/Subscription/SubscriptionBanner'
import Label from '../../Common/Typography/Label'
import Text from '../../Common/Typography/Text'
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
5 changes: 5 additions & 0 deletions src/shared/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
}
Loading