Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process 403 error from address/token/contract handle #2477

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

isstuev marked this conversation as resolved.
Show resolved Hide resolved
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),
isstuev marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 6 additions & 0 deletions ui/shared/AppError/AppError.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ test('status code 422', async({ render }) => {
await expect(component).toHaveScreenshot();
});

test('status code 403', async({ render }) => {
const error = { message: 'Test', cause: { status: 403 } } as Error;
const component = await render(<AppError error={ error }/>);
await expect(component).toHaveScreenshot();
});

test('status code 500', async({ render }) => {
const error = { message: 'Unknown error', cause: { status: 500 } } as Error;
const component = await render(<AppError error={ error }/>);
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading