Skip to content

Commit

Permalink
Process 403 error from address/token/contract handle
Browse files Browse the repository at this point in the history
  • Loading branch information
isstuev committed Dec 17, 2024
1 parent 3a400a1 commit 7bde717
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions icons/error-pages/403.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/icons/name.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
| "empty_search_result"
| "ENS_slim"
| "ENS"
| "error-pages/403"
| "error-pages/404"
| "error-pages/422"
| "error-pages/429"
Expand Down
19 changes: 16 additions & 3 deletions ui/address/utils/useAddressQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface Params {
isEnabled?: boolean;
}

const NO_RPC_FALLBACK_ERROR_CODES = [ 403 ];

export default function useAddressQuery({ hash, isEnabled = true }: Params): AddressQuery {
const [ isRefetchEnabled, setRefetchEnabled ] = React.useState(false);

Expand All @@ -35,6 +37,10 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
placeholderData: ADDRESS_INFO,
refetchOnMount: false,
retry: (failureCount, error) => {
if (error.status < 500) {
return false;
}

if (isRefetchEnabled) {
return false;
}
Expand Down Expand Up @@ -92,7 +98,7 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
};
},
placeholderData: [ GET_BALANCE ],
enabled: apiQuery.isError || apiQuery.errorUpdateCount > 0,
enabled: (apiQuery.isError || apiQuery.errorUpdateCount > 0) && !NO_RPC_FALLBACK_ERROR_CODES.includes(apiQuery.error?.status ?? 999),
retry: false,
refetchOnMount: false,
});
Expand All @@ -107,15 +113,22 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
} else if (!apiQuery.isError) {
setRefetchEnabled(false);
}
}, [ apiQuery.errorUpdateCount, apiQuery.isError, apiQuery.isPlaceholderData ]);
}, [ apiQuery.errorUpdateCount, apiQuery.isError, apiQuery.isPlaceholderData, apiQuery.error?.status ]);

React.useEffect(() => {
if (!rpcQuery.isPlaceholderData && !rpcQuery.data) {
setRefetchEnabled(false);
}
}, [ rpcQuery.data, rpcQuery.isPlaceholderData ]);

const isRpcQuery = Boolean((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0 && rpcQuery.data && publicClient);
const isRpcQuery = Boolean(
(apiQuery.isError || apiQuery.isPlaceholderData) &&
!NO_RPC_FALLBACK_ERROR_CODES.includes(apiQuery.error?.status ?? 999) &&
apiQuery.errorUpdateCount > 0 &&
rpcQuery.data &&
publicClient,
);

const query = isRpcQuery ? rpcQuery as UseQueryResult<Address, ResourceError<{ status: number }>> : apiQuery;

return {
Expand Down
4 changes: 4 additions & 0 deletions ui/shared/AppError/AppError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ interface Props {
}

const ERROR_TEXTS: Record<string, { title: string; text: string }> = {
'403': {
title: 'Forbidden',
text: 'Access to this resource is restricted.',
},
'404': {
title: 'Page not found',
text: 'This page is no longer explorable! If you are lost, use the search bar to find what you are looking for.',
Expand Down
1 change: 1 addition & 0 deletions ui/shared/AppError/AppErrorIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IconName } from 'ui/shared/IconSvg';
import IconSvg from 'ui/shared/IconSvg';

const ICONS: Record<string, IconName> = {
'403': 'error-pages/403',
'404': 'error-pages/404',
'422': 'error-pages/422',
'429': 'error-pages/429',
Expand Down
2 changes: 1 addition & 1 deletion ui/shared/AppError/isCustomAppError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ResourceError } from 'lib/api/resources';

// status codes when custom error screen should be shown
const CUSTOM_STATUS_CODES = [ 404, 422, 429 ];
const CUSTOM_STATUS_CODES = [ 403, 404, 422, 429 ];

export default function isCustomAppError(error: ResourceError<unknown>) {
return CUSTOM_STATUS_CODES.includes(error.status);
Expand Down

0 comments on commit 7bde717

Please sign in to comment.