Skip to content

Commit

Permalink
Merge pull request #1493 from oasisprotocol/lw/throw-5xx
Browse files Browse the repository at this point in the history
Automatically throw on 5xx error responses
  • Loading branch information
lukaw3d authored Aug 9, 2024
2 parents 7ad0ed8 + 92c9bce commit 331aed6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions .changelog/1493.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Automatically throw on 5xx error responses
6 changes: 3 additions & 3 deletions src/app/components/OfflineBanner/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { UseQueryResult } from '@tanstack/react-query'
export const useIsApiReachable = (
network: Network,
): { reachable: true } | { reachable: false; reason: 'userOffline' | 'apiOffline' } => {
const query = useGetStatus(network)
const query = useGetStatus(network, { query: { useErrorBoundary: false } })
if (query.isPaused) return { reachable: false, reason: 'userOffline' }
if (query.isFetched && !query.isSuccess) return { reachable: false, reason: 'apiOffline' }
return { reachable: true }
Expand Down Expand Up @@ -80,7 +80,7 @@ export const useConsensusFreshness = (
queryParams: { polling?: boolean } = {},
): FreshnessInfo => {
const query = useGetStatus(network, {
query: { refetchInterval: queryParams.polling ? 8000 : undefined },
query: { refetchInterval: queryParams.polling ? 8000 : undefined, useErrorBoundary: false },
})

return useFreshness(network, query)
Expand All @@ -95,7 +95,7 @@ export const useRuntimeFreshness = (
}

const query = useGetRuntimeStatus(scope.network, scope.layer, {
query: { refetchInterval: queryParams.polling ? 8000 : undefined },
query: { refetchInterval: queryParams.polling ? 8000 : undefined, useErrorBoundary: false },
})

return useFreshness(scope.network, query)
Expand Down
12 changes: 6 additions & 6 deletions src/app/data/oasis-account-names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { useQuery } from '@tanstack/react-query'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import {
Layer,
useGetConsensusAccountsAddresses,
Expand Down Expand Up @@ -65,19 +65,19 @@ const getOasisAccountsMetadata = async (network: Network, layer: Layer): Promise
export const useOasisAccountsMetadata = (
network: Network,
layer: Layer,
queryOptions: { enabled: boolean },
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
) => {
return useQuery(['oasisAccounts', network, layer], () => getOasisAccountsMetadata(network, layer), {
enabled: queryOptions.enabled,
staleTime: Infinity,
...queryOptions,
})
}

export const useOasisAccountMetadata = (
network: Network,
layer: Layer,
address: string,
queryOptions: { enabled: boolean },
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountMetadataInfo => {
const { isLoading, isError, error, data: allData } = useOasisAccountsMetadata(network, layer, queryOptions)
if (isError) {
Expand All @@ -94,14 +94,14 @@ export const useSearchForOasisAccountsByName = (
network: Network,
layer: Layer,
nameFragment: string,
queryOptions: { enabled: boolean },
queryOptions: { enabled: boolean } & UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountNameSearchResults => {
const {
isLoading: isMetadataLoading,
isError: isMetadataError,
error: metadataError,
data: namedAccounts,
} = useOasisAccountsMetadata(network, layer, queryOptions)
} = useOasisAccountsMetadata(network, layer, { useErrorBoundary: false, ...queryOptions })
if (isMetadataError) {
console.log('Failed to load Oasis account metadata', metadataError)
}
Expand Down
26 changes: 19 additions & 7 deletions src/app/data/pontusx-account-names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { useQuery } from '@tanstack/react-query'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import {
AccountMetadata,
AccountMap,
Expand All @@ -14,7 +14,12 @@ import { getOasisAddress } from '../utils/helpers'

const DATA_SOURCE_URL = 'https://github.com/deltaDAO/mvg-portal/main/pontusxAddresses.json'

const getPontusXAccountsMetadata = async () => {
type PontusXAccountsMetadata = {
map: AccountMap
list: AccountMetadata[]
}

const getPontusXAccountsMetadata = async (): Promise<PontusXAccountsMetadata> => {
const response = await axios.get(DATA_SOURCE_URL)
if (response.status !== 200) throw new Error("Couldn't load names")
if (!response.data) throw new Error("Couldn't load names")
Expand All @@ -34,16 +39,18 @@ const getPontusXAccountsMetadata = async () => {
}
}

export const usePontusXAccountsMetadata = (queryOptions: { enabled: boolean }) => {
export const usePontusXAccountsMetadata = (
queryOptions: UseQueryOptions<PontusXAccountsMetadata, unknown, PontusXAccountsMetadata, string[]>,
) => {
return useQuery(['pontusXNames'], getPontusXAccountsMetadata, {
enabled: queryOptions.enabled,
staleTime: Infinity,
...queryOptions,
})
}

export const usePontusXAccountMetadata = (
address: string,
queryOptions: { enabled: boolean },
queryOptions: UseQueryOptions<PontusXAccountsMetadata, unknown, PontusXAccountsMetadata, string[]>,
): AccountMetadataInfo => {
const { isLoading, isError, error, data: allData } = usePontusXAccountsMetadata(queryOptions)
if (isError) {
Expand All @@ -59,14 +66,19 @@ export const usePontusXAccountMetadata = (
export const useSearchForPontusXAccountsByName = (
network: Network,
nameFragment: string,
queryOptions: { enabled: boolean },
queryOptions: { enabled: boolean } & UseQueryOptions<
PontusXAccountsMetadata,
unknown,
PontusXAccountsMetadata,
string[]
>,
): AccountNameSearchRuntimeResults => {
const {
isLoading: isMetadataLoading,
isError: isMetadataError,
error: metadataError,
data: namedAccounts,
} = usePontusXAccountsMetadata(queryOptions)
} = usePontusXAccountsMetadata({ useErrorBoundary: false, ...queryOptions })
if (isMetadataError) {
console.log('Failed to load Pontus-X account names', metadataError)
}
Expand Down
1 change: 1 addition & 0 deletions src/coin-gecko/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function useGetTokenPricesFromGecko(tokenIds: string[], fiatCurrency: str
}),
{
staleTime,
useErrorBoundary: false,
},
)
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const queryClient = new QueryClient({
queries: {
refetchOnWindowFocus: false,
retry: false,
useErrorBoundary: (error: any) => {
// Automatically throw on 5xx errors. Components that want to handle
// errors should set `useErrorBoundary: false` in their queries.
return error.response?.status >= 500
},
},
},
})
Expand Down

0 comments on commit 331aed6

Please sign in to comment.