From 3700ee354c1b208186923489d711f1e6a878668b Mon Sep 17 00:00:00 2001 From: Nam Nguyen Date: Fri, 28 Jul 2023 17:30:21 +0700 Subject: [PATCH] format message --- src/hooks/kyberdao/index.tsx | 9 ++++++--- src/utils/errorMessage.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/utils/errorMessage.ts diff --git a/src/hooks/kyberdao/index.tsx b/src/hooks/kyberdao/index.tsx index ef3ff37f6e..5890974f67 100644 --- a/src/hooks/kyberdao/index.tsx +++ b/src/hooks/kyberdao/index.tsx @@ -30,6 +30,7 @@ import { useTransactionAdder } from 'state/transactions/hooks' import { TRANSACTION_TYPE } from 'state/transactions/type' import { calculateGasMargin } from 'utils' import { aggregateValue } from 'utils/array' +import { formatWalletErrorMessage } from 'utils/errorMessage' import { formatUnitsToFixed } from 'utils/formatBalance' import { sendEVMTransaction } from 'utils/sendTransaction' @@ -624,7 +625,7 @@ export function useClaimGasRefundRewards() { console.error('Claim error:', { error }) notify({ title: t`Claim Error`, - summary: error?.response?.data?.message || error?.message || 'Unknown error', + summary: error?.response?.data?.message || error?.message || t`Unknown error`, type: NotificationType.ERROR, }) throw error @@ -647,6 +648,7 @@ export function useClaimGasRefundRewards() { refetch() return tx.hash as string } catch (error) { + refetch() if (didUserReject(connector, error)) { notify({ title: t`Transaction rejected`, @@ -655,10 +657,11 @@ export function useClaimGasRefundRewards() { }) throw new Error('Transaction rejected.') } else { - console.error('Claim error:', { error }) + const message = formatWalletErrorMessage(error) + console.error('Claim error:', { error, message }) notify({ title: t`Claim Error`, - summary: error.message || 'Unknown error', + summary: message, type: NotificationType.ERROR, }) throw error diff --git a/src/utils/errorMessage.ts b/src/utils/errorMessage.ts new file mode 100644 index 0000000000..84e197007f --- /dev/null +++ b/src/utils/errorMessage.ts @@ -0,0 +1,18 @@ +import { t } from '@lingui/macro' + +function capitalizeFirstLetter(string: string) { + return string.charAt(0).toUpperCase() + string.slice(1) +} + +// to be add more patterns ... +const pattern1 = /{"originalError":.+"message":"execution reverted: ([^"]+)"/ +export function formatWalletErrorMessage(error: Error): string { + const message = error.message + if (message.length < 100) return message + + // extract & format long messages + const pattern1Result = pattern1.exec(message) + if (pattern1Result) return capitalizeFirstLetter(pattern1Result[1]) + + return t`Unknown error. Please try again.` +}