add detailed error message for adding feed

This commit is contained in:
Hongbo Wu 2023-07-20 14:10:12 +08:00
parent 06087c8578
commit d572a9e3e4
4 changed files with 37 additions and 22 deletions

View file

@ -9,9 +9,16 @@ type SubscribeResult = {
subscribe: Subscribe
}
enum SubscribeErrorCode {
BadRequest = 'BAD_REQUEST',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED',
AlreadySubscribed = 'ALREADY_SUBSCRIBED',
}
type Subscribe = {
subscriptions: Subscription[]
errorCodes?: unknown[]
subscriptions?: Subscription[]
errorCodes?: SubscribeErrorCode[]
}
export type SubscribeMutationInput = {
@ -22,7 +29,7 @@ export type SubscribeMutationInput = {
export async function subscribeMutation(
input: SubscribeMutationInput
): Promise<any | undefined> {
): Promise<SubscribeResult> {
const mutation = gql`
mutation Subscribe($input: SubscribeInput!) {
subscribe(input: $input) {
@ -39,9 +46,13 @@ export async function subscribeMutation(
`
try {
const data = (await gqlFetcher(mutation, { input })) as SubscribeResult
return data.subscribe.errorCodes ? undefined : data.subscribe
return data
} catch (error) {
console.log('subscribeMutation error', error)
return undefined
return {
subscribe: {
errorCodes: [SubscribeErrorCode.BadRequest],
},
}
}
}

View file

@ -7,9 +7,9 @@ interface UpdateSubscriptionResult {
}
export enum UpdateSubscriptionErrorCode {
BAD_REQUEST = 'BAD_REQUEST',
NOT_FOUND = 'NOT_FOUND',
UNAUTHORIZED = 'UNAUTHORIZED',
BadRequest = 'BAD_REQUEST',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED',
}
interface UpdateSubscription {
@ -52,7 +52,7 @@ export async function updateSubscriptionMutation(
console.log('updateSubscriptionMutation error', error)
return {
updateSubscription: {
errorCodes: [UpdateSubscriptionErrorCode.BAD_REQUEST],
errorCodes: [UpdateSubscriptionErrorCode.BadRequest],
},
}
}

View file

@ -25,7 +25,9 @@ const errorMessages: Record<string, string> = {
"Your sign up page has timed out, you'll be redirected to Google sign in page to authenticate again.",
'error.USER_EXISTS': 'User with this email exists already',
'error.UNKNOWN': 'An unknown error occurred',
'error.INVALID_PASSWORD': 'Invalid password. Password must be at least 8 chars.'
'error.INVALID_PASSWORD': 'Invalid password. Password must be at least 8 chars.',
'error.ALREADY_SUBSCRIBED': 'You are already subscribed to this feed',
'error.BAD_REQUEST': 'Bad request',
}
const loginPageMessages: Record<string, string> = {

View file

@ -14,6 +14,7 @@ import { SettingsLayout } from '../../../components/templates/SettingsLayout'
import { subscribeMutation } from '../../../lib/networking/mutations/subscribeMutation'
import { SubscriptionType } from '../../../lib/networking/queries/useGetSubscriptionsQuery'
import { showSuccessToast } from '../../../lib/toastHelpers'
import { formatMessage } from '../../../locales/en/messages'
// Styles
const Header = styled(Box, {
@ -30,20 +31,21 @@ export default function AddRssFeed(): JSX.Element {
const [feedUrl, setFeedUrl] = useState<string>('')
const subscribe = useCallback(async () => {
try {
const result = await subscribeMutation({
url: feedUrl,
subscriptionType: SubscriptionType.RSS,
const result = await subscribeMutation({
url: feedUrl,
subscriptionType: SubscriptionType.RSS,
})
if (result.subscribe.errorCodes) {
const errorMessage = formatMessage({
id: `error.${result.subscribe.errorCodes[0]}`,
})
if (result) {
router.push(`/settings/rss`)
showSuccessToast('New RSS feed has been added.')
} else {
setErrorMessage('There was an error adding new RSS feed.')
}
} catch (err) {
setErrorMessage('Error: ' + err)
setErrorMessage(`There was an error adding new RSS feed: ${errorMessage}`)
return
}
router.push(`/settings/rss`)
showSuccessToast('New RSS feed has been added.')
}, [feedUrl, router])
return (