Skip to content

Commit

Permalink
format message
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold committed Jul 28, 2023
1 parent 9942b80 commit 3700ee3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/hooks/kyberdao/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand All @@ -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`,
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/utils/errorMessage.ts
Original file line number Diff line number Diff line change
@@ -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.`
}

0 comments on commit 3700ee3

Please sign in to comment.