Skip to content

Commit aa9ba24

Browse files
authored
Add global sentry error logging for tan-query (#11998)
1 parent 45ce478 commit aa9ba24

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

packages/common/src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@ export * from './tan-query/wallets/useUSDCBalance'
127127

128128
// Saga fetch utils, remove when migration is complete
129129
export * from './tan-query/saga-utils'
130-
export * from './tan-query/utils/defaultRetryConfig'
130+
export * from './tan-query/utils/defaultConfig'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Query, QueryKey } from '@tanstack/react-query'
2+
3+
import {
4+
ErrorLevel,
5+
Feature,
6+
ReportToSentryArgs
7+
} from '~/models/ErrorReporting'
8+
9+
export const MAX_RETRIES = 3
10+
export const HTTP_STATUSES_TO_NOT_RETRY = [400, 401, 403, 404]
11+
12+
export const defaultRetryConfig = (failureCount: number, error: any) => {
13+
if (failureCount > MAX_RETRIES) {
14+
return false
15+
}
16+
17+
if (
18+
Object.hasOwnProperty.call(error, 'status') &&
19+
HTTP_STATUSES_TO_NOT_RETRY.includes(error.status)
20+
) {
21+
return false
22+
}
23+
24+
return true
25+
}
26+
27+
type ReportToSentry = (args: ReportToSentryArgs) => Promise<void>
28+
29+
export const queryErrorHandler = (
30+
err: unknown,
31+
query: Query<unknown, unknown, unknown, QueryKey>,
32+
reportToSentry: ReportToSentry
33+
) => {
34+
const error = err instanceof Error ? err : new Error(String(err))
35+
reportToSentry({
36+
error,
37+
level: ErrorLevel.Error,
38+
feature: Feature.TanQuery,
39+
name: `Query Error: ${query.queryKey[0] as string}`,
40+
additionalInfo: {
41+
queryHookMetadata: {
42+
queryKey: query.queryKey,
43+
isActive: query.isActive,
44+
isStale: query.isStale,
45+
isDisabled: query.isDisabled
46+
}
47+
}
48+
})
49+
}

packages/common/src/api/tan-query/utils/defaultRetryConfig.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/mobile/src/services/query-client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { defaultRetryConfig } from '@audius/common/api'
2-
import { QueryClient } from '@tanstack/react-query'
1+
import { defaultRetryConfig, queryErrorHandler } from '@audius/common/api'
2+
import { QueryCache, QueryClient } from '@tanstack/react-query'
33

44
import { env } from 'app/services/env'
5+
import { reportToSentry } from 'app/utils/reportToSentry'
56

67
export const queryClient = new QueryClient({
8+
// In order to have a default error catcher, we need to use a custom query cache
9+
// ref: https://tkdodo.eu/blog/react-query-error-handling
10+
queryCache: new QueryCache({
11+
onError(error, query) {
12+
queryErrorHandler(error, query, reportToSentry)
13+
}
14+
}),
715
defaultOptions: {
816
queries: {
917
// By default, tan-query will retry on 404s. We intentionally disable retrying on 400 type errors.

packages/web/src/services/query-client.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { defaultRetryConfig } from '@audius/common/api'
2-
import { QueryClient } from '@tanstack/react-query'
1+
import { defaultRetryConfig, queryErrorHandler } from '@audius/common/api'
2+
import { QueryCache, QueryClient } from '@tanstack/react-query'
3+
4+
import { reportToSentry } from 'store/errors/reportToSentry'
35

46
import { env } from './env'
57
export const queryClient = new QueryClient({
8+
// In order to have a default error catcher, we need to use a custom query cache
9+
// ref: https://tkdodo.eu/blog/react-query-error-handling
10+
queryCache: new QueryCache({
11+
onError(error, query) {
12+
queryErrorHandler(error, query, reportToSentry)
13+
}
14+
}),
615
defaultOptions: {
716
queries: {
817
// By default, tan-query will retry on 404s. We intentionally disable retrying on 400 type errors.

0 commit comments

Comments
 (0)