Skip to content

Commit

Permalink
Merge pull request #400 from 00labs/campaign-service-error-handling
Browse files Browse the repository at this point in the history
campaign service add error handling
  • Loading branch information
shan-57blocks authored Dec 17, 2024
2 parents 0d51315 + 30f40f4 commit ce0234e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 109 deletions.
83 changes: 60 additions & 23 deletions packages/huma-shared/src/services/CampaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gql } from 'graphql-request'
import { SolanaChainEnum, SolanaPoolInfo } from '../solana'
import { ChainEnum, NETWORK_TYPE } from '../utils/chain'
import { configUtil } from '../utils/config'
import { COMMON_ERROR_MESSAGE } from '../utils/const'
import { requestPost } from '../utils/request'
import { PoolInfoV2 } from '../v2/utils/pool'

Expand Down Expand Up @@ -96,20 +97,25 @@ function checkWalletOwnership(

return requestPost<{
data?: {
walletOwnership: boolean
walletOwnership: boolean & { errMessage: string }
}
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return undefined
console.log(res.errors)
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.walletOwnership?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}
return res.data?.walletOwnership
})
.catch((err) => {
console.error(err)
return undefined
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down Expand Up @@ -140,19 +146,24 @@ function getLeaderboard(
`

return requestPost<{
data?: { leaderboard: { data: LeaderboardItem[] } }
data?: { leaderboard: { data: LeaderboardItem[] & { errMessage: string } } }
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return undefined
console.log(res.errors)
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.leaderboard?.data?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}
return res.data?.leaderboard?.data
})
.catch((err) => {
console.error(err)
return undefined
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down Expand Up @@ -181,19 +192,25 @@ function getHumaAccountRanking(
`

return requestPost<{
data?: { myRankingEntry: LeaderboardItem }
data?: { myRankingEntry: LeaderboardItem & { errMessage: string } }
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return undefined
console.log(res.errors)
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.myRankingEntry?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}

return res.data?.myRankingEntry
})
.catch((err) => {
console.error(err)
return undefined
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down Expand Up @@ -225,14 +242,20 @@ function getHumaAccountPoints(
`

return requestPost<{
data?: { accountPoints: HumaAccountPoints }
data?: { accountPoints: HumaAccountPoints & { errMessage: string } }
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return undefined
console.log(res.errors)
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.accountPoints?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}

const accountPoints = res.data?.accountPoints
if (accountPoints) {
accountPoints.totalPoints =
Expand All @@ -245,7 +268,7 @@ function getHumaAccountPoints(
})
.catch((err) => {
console.error(err)
return undefined
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down Expand Up @@ -274,20 +297,26 @@ function getEstimatedPoints(
data?: {
calculateEstimatedPoints?: {
campaignPointsEstimations?: CampaignPoints[]
}
} & { errMessage: string }
}
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return []
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.calculateEstimatedPoints?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}

return res.data?.calculateEstimatedPoints?.campaignPointsEstimations ?? []
})
.catch((err) => {
console.error(err)
return []
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down Expand Up @@ -321,20 +350,28 @@ function updateHumaAccountPoints(

return requestPost<{
data?: {
updateAccountPoints?: { pointsAccumulated?: number }
updateAccountPoints?: { pointsAccumulated?: number } & {
errMessage: string
}
}
errors?: unknown
}>(url, JSON.stringify({ query }))
.then((res) => {
if (res.errors) {
console.error(res.errors)
return {}
console.log(res.errors)
throw new Error(COMMON_ERROR_MESSAGE)
}
const errMessage = res.data?.updateAccountPoints?.errMessage
if (errMessage) {
console.error(errMessage)
throw new Error(errMessage)
}

return res.data?.updateAccountPoints ?? {}
})
.catch((err) => {
console.error(err)
return {}
throw new Error(COMMON_ERROR_MESSAGE)
})
}

Expand Down
10 changes: 10 additions & 0 deletions packages/huma-shared/src/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ export const EARejectReason = 'Your wallet does not meet qualifications'
export const EARejectMessage =
'Based on your wallet transaction history your application was not approved.'

export const COMMON_ERROR_MESSAGE =
'Sorry, there was an error. Please try again.'

export enum CURRENCY_CODE {
USD = 840,
}

export const CAMPAIGN_REFERENCE_CODE = 'CAMPAIGN_REFERENCE_CODE'

export const BP_FACTOR_NUMBER = 10000

export enum HUMA_ACCOUNT_EXCEPTION {
AccountTokenNotFoundException = 'AccountTokenNotFoundException',
InvalidAccountTokenException = 'InvalidAccountTokenException',
WalletNotSignedInException = 'WalletNotSignedInException',
WalletNotCreatedException = 'WalletNotCreatedException',
}
15 changes: 7 additions & 8 deletions packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { CHAIN_TYPE } from '@huma-finance/shared'
import { CHAIN_TYPE, HUMA_ACCOUNT_EXCEPTION } from '@huma-finance/shared'
import axios, { HttpStatusCode } from 'axios'
import { useCallback, useState } from 'react'
import { useAuthErrorHandlingEvm } from './useAuthErrorHandlingEvm'
Expand Down Expand Up @@ -35,15 +35,14 @@ export const useAuthErrorHandling = (
axios.isAxiosError(error) &&
error.response?.status === HttpStatusCode.Unauthorized &&
[
'IdTokenNotFoundException',
'InvalidIdTokenException',
'WalletMismatchException',
'AccountTokenNotFoundException',
'InvalidAccountTokenException',
HUMA_ACCOUNT_EXCEPTION.AccountTokenNotFoundException,
HUMA_ACCOUNT_EXCEPTION.InvalidAccountTokenException,
].includes(error.response?.data?.detail?.type)

const isWalletNotCreatedError = error === 'WalletNotCreatedException'
const isWalletNotSignInError = error === 'WalletNotSignedInException'
const isWalletNotCreatedError =
error === HUMA_ACCOUNT_EXCEPTION.WalletNotCreatedException
const isWalletNotSignInError =
error === HUMA_ACCOUNT_EXCEPTION.WalletNotSignedInException

return {
isUnauthorizedError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ import {
isEmpty,
NETWORK_TYPE,
} from '@huma-finance/shared'
import {
SolanaPoolState,
txAtom,
useAuthErrorHandling,
useChainInfo,
} from '@huma-finance/web-shared'
import { SolanaPoolState, txAtom, useChainInfo } from '@huma-finance/web-shared'
import { Box, css, useTheme } from '@mui/material'
import { useResetAtom } from 'jotai/utils'
import React, { useCallback, useEffect, useState } from 'react'
import { useDispatch } from 'react-redux'

import { resetState, setError } from '../../../store/widgets.reducers'
import { resetState } from '../../../store/widgets.reducers'
import { BottomButton } from '../../BottomButton'
import { CongratulationsIcon, HumaPointsIcon, RibbonIcon } from '../../icons'
import { LoadingModal } from '../../LoadingModal'
Expand All @@ -30,9 +25,6 @@ enum STATE {
Congrats = 'Congrats',
}

const ERROR_MESSAGE =
'Failed to update wallet points. Be assured that your points will be added later.'

type Props = {
transactionHash: string
poolState: SolanaPoolState
Expand Down Expand Up @@ -61,83 +53,26 @@ export function PointsEarned({
)
const monthText =
lockupMonths > 1 ? `${lockupMonths} months` : `${lockupMonths} month`

const {
errorType,
setError: setAuthError,
isWalletOwnershipVerified,
isWalletOwnershipVerificationRequired,
} = useAuthErrorHandling(isDev)
const [walletOwnership, setWalletOwnership] = useState<boolean | undefined>()
const [state, setState] = useState<STATE>(STATE.Loading)

useEffect(() => {
if (isWalletOwnershipVerificationRequired) {
setState(STATE.Loading)
}
}, [isWalletOwnershipVerificationRequired])

useEffect(() => {
if (isWalletOwnershipVerified) {
setWalletOwnership(true)
}
}, [isWalletOwnershipVerified])

useEffect(() => {
const checkWalletOwnership = async () => {
if (account) {
const ownership = await CampaignService.checkWalletOwnership(
account,
const updateWalletPoints = async () => {
try {
const result = await CampaignService.updateHumaAccountPoints(
account!,
transactionHash,
chainId!,
networkType,
isDev,
)
setWalletOwnership(ownership)
if (!ownership) {
setAuthError('WalletNotSignedInException')
}
}
}
checkWalletOwnership()
}, [account, isDev, networkType, setAuthError])

useEffect(() => {
if (errorType === 'NotSignedIn') {
setState(STATE.SignIn)
} else if (errorType === 'UserRejected') {
dispatch(
setError({
errorMessage: 'User has rejected the transaction.',
}),
)
} else if (errorType === 'Other') {
dispatch(
setError({
errorMessage: ERROR_MESSAGE,
}),
)
}
}, [dispatch, errorType])

useEffect(() => {
const updateWalletPoints = async () => {
if (walletOwnership) {
try {
const result = await CampaignService.updateHumaAccountPoints(
account!,
transactionHash,
chainId!,
networkType,
isDev,
)
setPointsAccumulated(result.pointsAccumulated)
setState(STATE.Congrats)
} catch (error) {
console.error('Failed to update wallet points', error)
}
setPointsAccumulated(result.pointsAccumulated)
setState(STATE.Congrats)
} catch (error) {
console.error('Failed to update wallet points', error)
}
}
updateWalletPoints()
}, [account, chainId, isDev, networkType, transactionHash, walletOwnership])
}, [account, chainId, isDev, networkType, transactionHash])

const handleCloseModal = useCallback(() => {
reset()
Expand Down

0 comments on commit ce0234e

Please sign in to comment.