Skip to content

Commit 76c74d5

Browse files
authored
Process 403 error from address/token/contract handle (blockscout#2477)
* Process 403 error from address/token/contract handle * review fix
1 parent baeb0c8 commit 76c74d5

File tree

8 files changed

+28
-4
lines changed

8 files changed

+28
-4
lines changed

icons/error-pages/403.svg

+3
Loading

public/icons/name.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
| "empty_search_result"
5656
| "ENS_slim"
5757
| "ENS"
58+
| "error-pages/403"
5859
| "error-pages/404"
5960
| "error-pages/422"
6061
| "error-pages/429"

ui/address/utils/useAddressQuery.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface Params {
2525
isEnabled?: boolean;
2626
}
2727

28+
const NO_RPC_FALLBACK_ERROR_CODES = [ 403 ];
29+
2830
export default function useAddressQuery({ hash, isEnabled = true }: Params): AddressQuery {
2931
const [ isRefetchEnabled, setRefetchEnabled ] = React.useState(false);
3032

@@ -92,7 +94,7 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
9294
};
9395
},
9496
placeholderData: [ GET_BALANCE ],
95-
enabled: apiQuery.isError || apiQuery.errorUpdateCount > 0,
97+
enabled: (apiQuery.isError || apiQuery.errorUpdateCount > 0) && !(apiQuery.error?.status && NO_RPC_FALLBACK_ERROR_CODES.includes(apiQuery.error?.status)),
9698
retry: false,
9799
refetchOnMount: false,
98100
});
@@ -107,15 +109,22 @@ export default function useAddressQuery({ hash, isEnabled = true }: Params): Add
107109
} else if (!apiQuery.isError) {
108110
setRefetchEnabled(false);
109111
}
110-
}, [ apiQuery.errorUpdateCount, apiQuery.isError, apiQuery.isPlaceholderData ]);
112+
}, [ apiQuery.errorUpdateCount, apiQuery.isError, apiQuery.isPlaceholderData, apiQuery.error?.status ]);
111113

112114
React.useEffect(() => {
113115
if (!rpcQuery.isPlaceholderData && !rpcQuery.data) {
114116
setRefetchEnabled(false);
115117
}
116118
}, [ rpcQuery.data, rpcQuery.isPlaceholderData ]);
117119

118-
const isRpcQuery = Boolean((apiQuery.isError || apiQuery.isPlaceholderData) && apiQuery.errorUpdateCount > 0 && rpcQuery.data && publicClient);
120+
const isRpcQuery = Boolean(
121+
(apiQuery.isError || apiQuery.isPlaceholderData) &&
122+
!(apiQuery.error?.status && NO_RPC_FALLBACK_ERROR_CODES.includes(apiQuery.error?.status)) &&
123+
apiQuery.errorUpdateCount > 0 &&
124+
rpcQuery.data &&
125+
publicClient,
126+
);
127+
119128
const query = isRpcQuery ? rpcQuery as UseQueryResult<Address, ResourceError<{ status: number }>> : apiQuery;
120129

121130
return {

ui/shared/AppError/AppError.pw.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ test('status code 422', async({ render }) => {
2020
await expect(component).toHaveScreenshot();
2121
});
2222

23+
test('status code 403', async({ render }) => {
24+
const error = { message: 'Test', cause: { status: 403 } } as Error;
25+
const component = await render(<AppError error={ error }/>);
26+
await expect(component).toHaveScreenshot();
27+
});
28+
2329
test('status code 500', async({ render }) => {
2430
const error = { message: 'Unknown error', cause: { status: 500 } } as Error;
2531
const component = await render(<AppError error={ error }/>);

ui/shared/AppError/AppError.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ interface Props {
2424
}
2525

2626
const ERROR_TEXTS: Record<string, { title: string; text: string }> = {
27+
'403': {
28+
title: 'Forbidden',
29+
text: 'Access to this resource is restricted.',
30+
},
2731
'404': {
2832
title: 'Page not found',
2933
text: 'This page is no longer explorable! If you are lost, use the search bar to find what you are looking for.',

ui/shared/AppError/AppErrorIcon.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { IconName } from 'ui/shared/IconSvg';
44
import IconSvg from 'ui/shared/IconSvg';
55

66
const ICONS: Record<string, IconName> = {
7+
'403': 'error-pages/403',
78
'404': 'error-pages/404',
89
'422': 'error-pages/422',
910
'429': 'error-pages/429',
Loading

ui/shared/AppError/isCustomAppError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ResourceError } from 'lib/api/resources';
22

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

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

0 commit comments

Comments
 (0)