Skip to content

Commit cf21c13

Browse files
authored
Improve sentry log (#2553)
* refactor Sentry error * fix to warn only known error patterns * add some comment to ignored issues
1 parent 5b736c2 commit cf21c13

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

src/hooks/useEthersProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function clientToProvider(client?: Client<Transport, Chain>, chainId?: number) {
1313
}
1414
const { chain, transport } = client
1515

16-
const ensAddress = chain.contracts?.ensRegistry?.address
16+
const ensAddress = chain?.contracts?.ensRegistry?.address
1717
const network = chain
1818
? {
1919
chainId: chain.id,

src/hooks/useLogin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const useLogin = (autoLogin = false) => {
7171
} catch (error) {
7272
const e = new Error('createProfile Error', { cause: error })
7373
e.name = 'createProfile Error'
74-
captureException(e, { extra: { walletAddress, account } })
74+
captureException(e, { extra: { walletAddress, account }, level: 'warning' })
7575
setProfile({ profile: undefined, isAnonymous, account })
7676
}
7777
},

src/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,29 @@ if (ENV_LEVEL > ENV_TYPE.LOCAL) {
5050
environment: 'production',
5151
ignoreErrors: ['AbortError'],
5252
integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
53-
tracesSampleRate: 0.1,
53+
tracesSampleRate: 1.0,
5454
normalizeDepth: 5,
55-
replaysSessionSampleRate: 0.1,
55+
replaysSessionSampleRate: 1.0,
5656
replaysOnErrorSampleRate: 1.0,
57+
beforeSend(event, hint) {
58+
const error = hint?.originalException as Error
59+
const { name, message } = error
60+
if (
61+
(name === 'TypeError' && message === 'Load failed') || // Almost come from mobile safari fetch API issues
62+
(name === 'ChunkLoadError' && message.includes('Failed to fetch')) || // https://sentry.io/answers/chunk-load-errors-javascript/
63+
(name === 'Error' && message.includes('Java object is gone')) || // coming from the WebView to Java bridge in Chrome, something went wrong with Chrome Mobile WebView from some Android devices
64+
(name === 'UnhandledRejection' && message.includes('Non-Error promise rejection captured with value')) ||
65+
(name === '<unknown>' && message.includes('Non-Error promise rejection captured with value')) || // this always happens when a some external library throws an error, checked with all issues in Sentry logs
66+
(name === '<unknown>' && message.includes('Object captured as promise rejection with keys')) // this always happens when a some external library throws an error, checked with all issues in Sentry logs
67+
)
68+
return null
69+
70+
if (name === 'TypeError' && message.includes('Failed to fetch')) {
71+
event.level = 'warning'
72+
}
73+
74+
return event
75+
},
5776
})
5877
Sentry.setTag('request_id', sentryRequestId)
5978
Sentry.setTag('version', TAG)

src/state/transactions/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const initialState: TransactionState = {}
2424
const clearOldTransactions = (transactions: GroupedTxsByHash | undefined): GroupedTxsByHash | undefined => {
2525
if (!transactions) return undefined
2626
const chainTxs = Object.values(transactions ?? {}).filter(Boolean) as TransactionDetails[][]
27-
chainTxs.sort((a, b) => a[0].addedTime - b[0].addedTime)
27+
chainTxs.sort((a, b) => (a[0]?.addedTime || 0) - (b[0]?.addedTime || 0))
2828
const slicedChainTxs = chainTxs.slice(-10).filter(tx => tx[0].addedTime > Date.now() - 7 * 24 * 60 * 60 * 1000)
2929
const result = slicedChainTxs.reduce((acc, cur) => ({ ...acc, [cur[0].hash]: cur }), {}) as GroupedTxsByHash
3030
return result

src/utils/errorMessage.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,32 @@ import { capitalizeFirstLetter } from 'utils/string'
66
const matchPatterns = (patterns: string[], error: string) =>
77
patterns.some(pattern => error.toLowerCase().includes(pattern.toLowerCase()))
88

9+
export const knownPatterns = {
10+
insufficient_erc20_balance: 'Insufficient ERC20 balance to pay gas fee',
11+
router_expired: 'An error occurred. Refresh the page and try again.',
12+
already_pending: 'Pending request(s), please approve it in your wallet.',
13+
mintotalamountout: 'An error occurred. Try refreshing the price rate or increase max slippage.',
14+
from_address_mismatch: 'The requested account and/or method has not been authorized by the user.',
15+
insufficient_funds: 'Your current balance falls short of covering the required gas fee.',
16+
swap_failed:
17+
'An error occurred. Refresh the page and try again. If the issue still persists, it might be an issue with your RPC node settings in Metamask.',
18+
underlying_network_changed: 'Your chain is mismatched, please make sure your wallet is switch to the expected chain.',
19+
user_rejected: 'User rejected the transaction.',
20+
insufficient: 'An error occurred. Please try increasing max slippage.',
21+
permit: 'An error occurred. Invalid Permit Signature.',
22+
burn_amount_exceeds_balance:
23+
'Insufficient fee rewards amount, try to remove your liquidity without claiming fees for now and you can try to claim it later.',
24+
object_object: 'Something went wrong. Please try again.',
25+
}
26+
927
function parseKnownPattern(text: string): string | undefined {
1028
const error = text?.toLowerCase?.() || ''
1129

12-
if (matchPatterns(['insufficient erc20 balance'], error)) return t`Insufficient ERC20 balance to pay gas fee`
30+
if (matchPatterns(['insufficient erc20 balance'], error)) return knownPatterns.insufficient_erc20_balance
1331

14-
if (!error || error.includes('router: expired')) return t`An error occurred. Refresh the page and try again.`
32+
if (!error || error.includes('router: expired')) return knownPatterns.router_expired
1533

16-
if (matchPatterns(['already pending'], error)) return t`Pending request(s), please approve it in your wallet.`
34+
if (matchPatterns(['already pending'], error)) return knownPatterns.already_pending
1735

1836
if (
1937
matchPatterns(
@@ -28,41 +46,38 @@ function parseKnownPattern(text: string): string | undefined {
2846
error,
2947
)
3048
)
31-
return t`An error occurred. Try refreshing the price rate or increase max slippage.`
49+
return knownPatterns.mintotalamountout
3250

3351
if (
3452
matchPatterns(
3553
['The requested account and/or method has not been authorized by the user', 'From address mismatch'],
3654
error,
3755
)
3856
)
39-
return t`The requested account and/or method has not been authorized by the user.`
57+
return knownPatterns.from_address_mismatch
4058

4159
if (
4260
matchPatterns(
4361
['insufficient funds for intrinsic transaction cost', 'OutOfFund', 'insufficient balance for transfer'],
4462
error,
4563
)
4664
)
47-
return t`Your current balance falls short of covering the required gas fee.`
65+
return knownPatterns.insufficient_funds
4866

49-
if (matchPatterns(['header not found', 'swap failed'], error))
50-
return t`An error occurred. Refresh the page and try again. If the issue still persists, it might be an issue with your RPC node settings in Metamask.`
67+
if (matchPatterns(['header not found', 'swap failed'], error)) return knownPatterns.swap_failed
5168

52-
if (matchPatterns(['underlying network changed'], error))
53-
return t`Your chain is mismatched, please make sure your wallet is switch to the expected chain.`
69+
if (matchPatterns(['underlying network changed'], error)) return knownPatterns.underlying_network_changed
5470

55-
if (didUserReject(error)) return t`User rejected the transaction.`
71+
if (didUserReject(error)) return knownPatterns.user_rejected
5672

5773
// classic/elastic remove liquidity error
58-
if (matchPatterns(['insufficient'], error)) return t`An error occurred. Please try increasing max slippage.`
74+
if (matchPatterns(['insufficient'], error)) return knownPatterns.insufficient
5975

60-
if (matchPatterns(['permit'], error)) return t`An error occurred. Invalid Permit Signature.`
76+
if (matchPatterns(['permit'], error)) return knownPatterns.permit
6177

62-
if (matchPatterns(['burn amount exceeds balance'], error))
63-
return t`Insufficient fee rewards amount, try to remove your liquidity without claiming fees for now and you can try to claim it later.`
78+
if (matchPatterns(['burn amount exceeds balance'], error)) return knownPatterns.burn_amount_exceeds_balance
6479

65-
if (error === '[object Object]') return t`Something went wrong. Please try again.`
80+
if (error === '[object Object]') return knownPatterns.object_object
6681

6782
return undefined
6883
}

src/utils/sentry.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Deferrable } from 'ethers/lib/utils'
44

55
import { didUserReject } from 'constants/connectors/utils'
66

7-
import { friendlyError } from './errorMessage'
7+
import { friendlyError, knownPatterns } from './errorMessage'
88

99
export enum ErrorName {
1010
LimitOrderError = 'LimitOrderError',
@@ -34,8 +34,14 @@ export function captureSwapError(error: TransactionError) {
3434
? 'returnAmountIsNotEnough'
3535
: 'other'
3636

37+
const level = Object.keys(knownPatterns)
38+
.map(key => knownPatterns[key])
39+
.includes(friendlyErrorResult)
40+
? 'warning'
41+
: 'error'
42+
3743
captureException(e, {
38-
level: 'fatal',
44+
level,
3945
extra: { rawData: error.rawData },
4046
tags: {
4147
type: tag,

0 commit comments

Comments
 (0)