From 29e0452f02c73bf266c7f38e32276fe5f8dd9e83 Mon Sep 17 00:00:00 2001 From: shan-57blocks Date: Fri, 13 Dec 2024 15:13:43 +0800 Subject: [PATCH 1/4] campaign service add error handling --- .../src/services/CampaignService.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/huma-shared/src/services/CampaignService.ts b/packages/huma-shared/src/services/CampaignService.ts index fc90a5b..688a2fd 100644 --- a/packages/huma-shared/src/services/CampaignService.ts +++ b/packages/huma-shared/src/services/CampaignService.ts @@ -96,12 +96,12 @@ function checkWalletOwnership( return requestPost<{ data?: { - walletOwnership: boolean + walletOwnership: boolean & { errMessage: string } } errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors) { + if (res.errors || res.data?.walletOwnership?.errMessage) { console.error(res.errors) return undefined } @@ -140,11 +140,11 @@ 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) { + if (res.errors || res.data?.leaderboard?.data?.errMessage) { console.error(res.errors) return undefined } @@ -181,11 +181,11 @@ function getHumaAccountRanking( ` return requestPost<{ - data?: { myRankingEntry: LeaderboardItem } + data?: { myRankingEntry: LeaderboardItem & { errMessage: string } } errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors) { + if (res.errors || res.data?.myRankingEntry?.errMessage) { console.error(res.errors) return undefined } @@ -225,11 +225,11 @@ function getHumaAccountPoints( ` return requestPost<{ - data?: { accountPoints: HumaAccountPoints } + data?: { accountPoints: HumaAccountPoints & { errMessage: string } } errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors) { + if (res.errors || res.data?.accountPoints?.errMessage) { console.error(res.errors) return undefined } @@ -274,12 +274,12 @@ function getEstimatedPoints( data?: { calculateEstimatedPoints?: { campaignPointsEstimations?: CampaignPoints[] - } + } & { errMessage: string } } errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors) { + if (res.errors || res.data?.calculateEstimatedPoints?.errMessage) { console.error(res.errors) return [] } @@ -321,12 +321,14 @@ function updateHumaAccountPoints( return requestPost<{ data?: { - updateAccountPoints?: { pointsAccumulated?: number } + updateAccountPoints?: { pointsAccumulated?: number } & { + errMessage: string + } } errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors) { + if (res.errors || res.data?.updateAccountPoints?.errMessage) { console.error(res.errors) return {} } From 9306114f763dfab53e49eddaffc9596fa2039d2f Mon Sep 17 00:00:00 2001 From: shan-57blocks Date: Fri, 13 Dec 2024 17:17:12 +0800 Subject: [PATCH 2/4] campaign service error handling --- .../src/services/CampaignService.ts | 81 +++++++++++++------ packages/huma-shared/src/utils/const.ts | 13 +++ .../src/hooks/useAuthErrorHandling/index.ts | 18 +++-- .../Lend/solanaSupply/8-PointsEarned.tsx | 21 +++-- 4 files changed, 94 insertions(+), 39 deletions(-) diff --git a/packages/huma-shared/src/services/CampaignService.ts b/packages/huma-shared/src/services/CampaignService.ts index 688a2fd..609112e 100644 --- a/packages/huma-shared/src/services/CampaignService.ts +++ b/packages/huma-shared/src/services/CampaignService.ts @@ -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' @@ -101,15 +102,20 @@ function checkWalletOwnership( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.walletOwnership?.errMessage) { - console.error(res.errors) - return undefined + if (res.errors) { + 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) }) } @@ -144,15 +150,20 @@ function getLeaderboard( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.leaderboard?.data?.errMessage) { - console.error(res.errors) - return undefined + if (res.errors) { + 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) }) } @@ -185,15 +196,21 @@ function getHumaAccountRanking( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.myRankingEntry?.errMessage) { - console.error(res.errors) - return undefined + if (res.errors) { + 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) }) } @@ -229,10 +246,16 @@ function getHumaAccountPoints( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.accountPoints?.errMessage) { - console.error(res.errors) - return undefined + if (res.errors) { + 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 = @@ -245,7 +268,7 @@ function getHumaAccountPoints( }) .catch((err) => { console.error(err) - return undefined + throw new Error(COMMON_ERROR_MESSAGE) }) } @@ -279,15 +302,21 @@ function getEstimatedPoints( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.calculateEstimatedPoints?.errMessage) { + 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) }) } @@ -328,15 +357,21 @@ function updateHumaAccountPoints( errors?: unknown }>(url, JSON.stringify({ query })) .then((res) => { - if (res.errors || res.data?.updateAccountPoints?.errMessage) { - console.error(res.errors) - return {} + if (res.errors) { + 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) }) } diff --git a/packages/huma-shared/src/utils/const.ts b/packages/huma-shared/src/utils/const.ts index fe5b20b..4eb9d9f 100644 --- a/packages/huma-shared/src/utils/const.ts +++ b/packages/huma-shared/src/utils/const.ts @@ -3,6 +3,9 @@ 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, } @@ -10,3 +13,13 @@ export enum CURRENCY_CODE { export const CAMPAIGN_REFERENCE_CODE = 'CAMPAIGN_REFERENCE_CODE' export const BP_FACTOR_NUMBER = 10000 + +export enum HUMA_ACCOUNT_EXCEPTION { + IdTokenNotFoundException = 'IdTokenNotFoundException', + InvalidIdTokenException = 'InvalidIdTokenException', + WalletMismatchException = 'WalletMismatchException', + AccountTokenNotFoundException = 'AccountTokenNotFoundException', + InvalidAccountTokenException = 'InvalidAccountTokenException', + WalletNotSignedInException = 'WalletNotSignedInException', + WalletNotCreatedException = 'WalletNotCreatedException', +} diff --git a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts index efca4b5..68005fd 100644 --- a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts +++ b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts @@ -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' @@ -35,15 +35,17 @@ export const useAuthErrorHandling = ( axios.isAxiosError(error) && error.response?.status === HttpStatusCode.Unauthorized && [ - 'IdTokenNotFoundException', - 'InvalidIdTokenException', - 'WalletMismatchException', - 'AccountTokenNotFoundException', - 'InvalidAccountTokenException', + HUMA_ACCOUNT_EXCEPTION.IdTokenNotFoundException, + HUMA_ACCOUNT_EXCEPTION.InvalidIdTokenException, + HUMA_ACCOUNT_EXCEPTION.WalletMismatchException, + 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, diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx index 4b4ccf1..c6737a8 100644 --- a/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx +++ b/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx @@ -4,6 +4,7 @@ import { checkIsDev, CloseModalOptions, formatNumber, + HUMA_ACCOUNT_EXCEPTION, isEmpty, NETWORK_TYPE, } from '@huma-finance/shared' @@ -86,14 +87,18 @@ export function PointsEarned({ useEffect(() => { const checkWalletOwnership = async () => { if (account) { - const ownership = await CampaignService.checkWalletOwnership( - account, - networkType, - isDev, - ) - setWalletOwnership(ownership) - if (!ownership) { - setAuthError('WalletNotSignedInException') + try { + const ownership = await CampaignService.checkWalletOwnership( + account, + networkType, + isDev, + ) + setWalletOwnership(ownership) + if (!ownership) { + setAuthError(HUMA_ACCOUNT_EXCEPTION.WalletNotSignedInException) + } + } catch (error) { + setAuthError(HUMA_ACCOUNT_EXCEPTION.WalletNotSignedInException) } } } From e934383297662b3366ad50d4fe29f88f657aca34 Mon Sep 17 00:00:00 2001 From: shan Date: Mon, 16 Dec 2024 14:01:17 +0800 Subject: [PATCH 3/4] remove unnecessary wallet check and exception code --- packages/huma-shared/src/utils/const.ts | 2 - .../src/hooks/useAuthErrorHandling/index.ts | 2 - .../Lend/solanaSupply/8-PointsEarned.tsx | 100 +++--------------- 3 files changed, 15 insertions(+), 89 deletions(-) diff --git a/packages/huma-shared/src/utils/const.ts b/packages/huma-shared/src/utils/const.ts index 4eb9d9f..f143fa4 100644 --- a/packages/huma-shared/src/utils/const.ts +++ b/packages/huma-shared/src/utils/const.ts @@ -15,8 +15,6 @@ export const CAMPAIGN_REFERENCE_CODE = 'CAMPAIGN_REFERENCE_CODE' export const BP_FACTOR_NUMBER = 10000 export enum HUMA_ACCOUNT_EXCEPTION { - IdTokenNotFoundException = 'IdTokenNotFoundException', - InvalidIdTokenException = 'InvalidIdTokenException', WalletMismatchException = 'WalletMismatchException', AccountTokenNotFoundException = 'AccountTokenNotFoundException', InvalidAccountTokenException = 'InvalidAccountTokenException', diff --git a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts index 68005fd..88271a7 100644 --- a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts +++ b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts @@ -35,8 +35,6 @@ export const useAuthErrorHandling = ( axios.isAxiosError(error) && error.response?.status === HttpStatusCode.Unauthorized && [ - HUMA_ACCOUNT_EXCEPTION.IdTokenNotFoundException, - HUMA_ACCOUNT_EXCEPTION.InvalidIdTokenException, HUMA_ACCOUNT_EXCEPTION.WalletMismatchException, HUMA_ACCOUNT_EXCEPTION.AccountTokenNotFoundException, HUMA_ACCOUNT_EXCEPTION.InvalidAccountTokenException, diff --git a/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx b/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx index c6737a8..4971959 100644 --- a/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx +++ b/packages/huma-widget/src/components/Lend/solanaSupply/8-PointsEarned.tsx @@ -4,22 +4,16 @@ import { checkIsDev, CloseModalOptions, formatNumber, - HUMA_ACCOUNT_EXCEPTION, 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' @@ -31,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 @@ -62,87 +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() const [state, setState] = useState(STATE.Loading) - useEffect(() => { - if (isWalletOwnershipVerificationRequired) { - setState(STATE.Loading) - } - }, [isWalletOwnershipVerificationRequired]) - - useEffect(() => { - if (isWalletOwnershipVerified) { - setWalletOwnership(true) - } - }, [isWalletOwnershipVerified]) - - useEffect(() => { - const checkWalletOwnership = async () => { - if (account) { - try { - const ownership = await CampaignService.checkWalletOwnership( - account, - networkType, - isDev, - ) - setWalletOwnership(ownership) - if (!ownership) { - setAuthError(HUMA_ACCOUNT_EXCEPTION.WalletNotSignedInException) - } - } catch (error) { - setAuthError(HUMA_ACCOUNT_EXCEPTION.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) - } + 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) } } updateWalletPoints() - }, [account, chainId, isDev, networkType, transactionHash, walletOwnership]) + }, [account, chainId, isDev, networkType, transactionHash]) const handleCloseModal = useCallback(() => { reset() From 30f40f41b4c420bc6cff2eeebd4365d5a4a36f51 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 17 Dec 2024 15:46:35 +0800 Subject: [PATCH 4/4] remove WalletMismatchException --- packages/huma-shared/src/utils/const.ts | 1 - packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/huma-shared/src/utils/const.ts b/packages/huma-shared/src/utils/const.ts index f143fa4..2e003b7 100644 --- a/packages/huma-shared/src/utils/const.ts +++ b/packages/huma-shared/src/utils/const.ts @@ -15,7 +15,6 @@ export const CAMPAIGN_REFERENCE_CODE = 'CAMPAIGN_REFERENCE_CODE' export const BP_FACTOR_NUMBER = 10000 export enum HUMA_ACCOUNT_EXCEPTION { - WalletMismatchException = 'WalletMismatchException', AccountTokenNotFoundException = 'AccountTokenNotFoundException', InvalidAccountTokenException = 'InvalidAccountTokenException', WalletNotSignedInException = 'WalletNotSignedInException', diff --git a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts index 88271a7..2f971b9 100644 --- a/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts +++ b/packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts @@ -35,7 +35,6 @@ export const useAuthErrorHandling = ( axios.isAxiosError(error) && error.response?.status === HttpStatusCode.Unauthorized && [ - HUMA_ACCOUNT_EXCEPTION.WalletMismatchException, HUMA_ACCOUNT_EXCEPTION.AccountTokenNotFoundException, HUMA_ACCOUNT_EXCEPTION.InvalidAccountTokenException, ].includes(error.response?.data?.detail?.type)