fix(explorer): pass through tendermint error responses from usefetch (#5507)

This commit is contained in:
Edd 2023-12-20 08:08:56 +00:00 committed by GitHub
parent 5aaeb87059
commit 362a2031c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 12 deletions

View File

@ -54,7 +54,7 @@ const Block = () => {
</Button> </Button>
</Link> </Link>
</div> </div>
{blockData && ( {blockData && 'result' in blockData && (
<> <>
<TableWithTbody className="mb-8"> <TableWithTbody className="mb-8">
<TableRow modifier="bordered"> <TableRow modifier="bordered">

View File

@ -95,7 +95,7 @@ export const ProtocolUpgradeProposalContainer = () => {
time={ time={
pending && time ? ( pending && time ? (
convertToCountdownString(time, '0:00:00:00') convertToCountdownString(time, '0:00:00:00')
) : blockInfo?.result ? ( ) : blockInfo && 'result' in blockInfo && blockInfo?.result ? (
<span title={blockInfo.result.block.header.time}> <span title={blockInfo.result.block.header.time}>
{formatDateWithLocalTimezone( {formatDateWithLocalTimezone(
new Date(blockInfo.result.block.header.time) new Date(blockInfo.result.block.header.time)

View File

@ -68,10 +68,13 @@ export const useBlockRising = (skip = false) => {
} }
); );
const heights = compact([ const heights = compact([...results.map((r) => r?.blockHeight)]);
...results.map((r) => r?.blockHeight),
blockInfo?.result.block.header.height, // Handles TendermintErrorResponses
]); if (blockInfo && 'result' in blockInfo) {
heights.push(blockInfo.result.block.header.height);
}
const current = max(heights); const current = max(heights);
if (current && Number(current) > prev) { if (current && Number(current) > prev) {
setBlock(Number(current)); setBlock(Number(current));

View File

@ -76,16 +76,26 @@ export const useFetch = <T>(
...options, ...options,
body: body ? body : options?.body, body: body ? body : options?.body,
}); });
if (!response.ok) {
data = (await response.json()) as T;
if (!response.ok && !data) {
throw new Error(response.statusText); throw new Error(response.statusText);
} }
data = (await response.json()) as T;
// @ts-ignore - 'error' in data // @ts-ignore - 'error' in data
if (data && 'error' in data) { if (data && data.error) {
// Explicit check for TendermintErrorResponse style error
// @ts-ignore - 'error' in data
if (data.error.data) {
// @ts-ignore - 'error' in data
throw new Error(data.error.data);
}
// @ts-ignore - data.error // @ts-ignore - data.error
throw new Error(data.error); throw new Error(data.error);
} }
if (cancelRequest.current) return; if (cancelRequest.current) return;
dispatch({ type: ActionType.FETCHED, payload: data }); dispatch({ type: ActionType.FETCHED, payload: data });

View File

@ -1,6 +1,11 @@
import { useEnvironment } from '@vegaprotocol/environment'; import { useEnvironment } from '@vegaprotocol/environment';
import { useFetch } from '@vegaprotocol/react-helpers'; import { useFetch } from '@vegaprotocol/react-helpers';
import { type TendermintBlockResponse } from '../types'; import type {
TendermintBlockResponse,
TendermintErrorResponse,
} from '../types';
type TendermintResponse = TendermintBlockResponse | TendermintErrorResponse;
export const useBlockInfo = (blockHeight?: number, canFetch = true) => { export const useBlockInfo = (blockHeight?: number, canFetch = true) => {
const { TENDERMINT_URL } = useEnvironment(); const { TENDERMINT_URL } = useEnvironment();
@ -10,7 +15,7 @@ export const useBlockInfo = (blockHeight?: number, canFetch = true) => {
TENDERMINT_URL && blockHeight && !isNaN(blockHeight) && canFetch TENDERMINT_URL && blockHeight && !isNaN(blockHeight) && canFetch
); );
const { state, refetch } = useFetch<TendermintBlockResponse>( const { state, refetch } = useFetch<TendermintResponse>(
url, url,
{ cache: 'force-cache' }, { cache: 'force-cache' },
canFetchData canFetchData

View File

@ -7,6 +7,16 @@ export type TendermintBlockResponse = {
}; };
}; };
export type TendermintErrorResponse = {
jsonrpc: string;
id: number;
error: {
code: number;
message: string;
data: string;
};
};
type Id = { type Id = {
hash: string; hash: string;
parts: { parts: {
@ -34,7 +44,7 @@ type Header = {
proposer_address: string; proposer_address: string;
}; };
type Block = { export type Block = {
header: Header; header: Header;
data: { data: {
txs: string[]; txs: string[];