Skip to content

Commit

Permalink
fix(no-tx-addr): added custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
BLuEScioN committed Jun 25, 2024
1 parent 22bb906 commit 8c974a3
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 31 deletions.
20 changes: 19 additions & 1 deletion src/app/_components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import { ErrorBoundary } from 'react-error-boundary';
import { ExplorerError } from '../../common/types/Error';
import { ErrorBox } from './ErrorBox';

export class RetryableError extends Error {
constructor(message: string) {
super(message);
this.name = 'RetryableError';

Check warning on line 11 in src/app/_components/ErrorBoundary.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/ErrorBoundary.tsx#L8-L11

Added lines #L8 - L11 were not covered by tests
}
}

export class NonRetryableError extends Error {
constructor(message: string) {
super(message);
this.name = 'NonRetryableError';

Check warning on line 18 in src/app/_components/ErrorBoundary.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/ErrorBoundary.tsx#L15-L18

Added lines #L15 - L18 were not covered by tests
}
}

interface ErrorBoundaryProps<WrapperProps extends PropsWithChildren<Record<string, unknown>>> {
Wrapper?: ComponentType<WrapperProps>;
wrapperProps?: WrapperProps;
Expand All @@ -27,6 +41,10 @@ export function ExplorerErrorBoundary<WrapperProps extends PropsWithChildren<unk
{({ reset }) => (
<ErrorBoundary
fallbackRender={({ error, resetErrorBoundary }) => {
let showTryAgain = tryAgainButton;

Check warning on line 44 in src/app/_components/ErrorBoundary.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/ErrorBoundary.tsx#L44

Added line #L44 was not covered by tests
if (error instanceof NonRetryableError) {
showTryAgain = false;

Check warning on line 46 in src/app/_components/ErrorBoundary.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/ErrorBoundary.tsx#L46

Added line #L46 was not covered by tests
}
return (
<Wrapper {...wrapperProps}>
{renderContent ? (
Expand All @@ -38,7 +56,7 @@ export function ExplorerErrorBoundary<WrapperProps extends PropsWithChildren<unk
reset();
resetErrorBoundary();
}}
tryAgainButton={tryAgainButton}
tryAgainButton={showTryAgain}
homeButton={homeButton}
/>
)}
Expand Down
21 changes: 16 additions & 5 deletions src/app/address/[principal]/PageClient.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
'use client';

import * as React from 'react';

import { useSuspenseAccountBalance } from '../../../common/queries/useAccountBalance';
import { useAddressNonces } from '../../../common/queries/useAddressNonces';
import { hasTokenBalance } from '../../../common/utils/accounts';
import { AddressTxListTabs } from '../../../features/txs-list/tabs/AddressTxListTabs';
import { Grid, GridProps } from '../../../ui/Grid';

Check warning on line 7 in src/app/address/[principal]/PageClient.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/address/[principal]/PageClient.tsx#L7

Added line #L7 was not covered by tests
import { Stack } from '../../../ui/components';
import { PageTitle } from '../../_components/PageTitle';
import { AddressSummary } from './AddressSummary';
import { StxBalance } from './StxBalance';
import { TokenBalanceCard } from './TokenBalanceCard';
import { Wrapper } from './Wrapper';

export function AddressPageLayout(props: GridProps) {

Check warning on line 14 in src/app/address/[principal]/PageClient.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/address/[principal]/PageClient.tsx#L14

Added line #L14 was not covered by tests
return (
<Grid
gridColumnGap="32px"
gridTemplateColumns={['100%', '100%', 'repeat(1, calc(100% - 352px) 320px)']}
gridRowGap={['32px', '32px', 'unset']}
maxWidth="100%"
alignItems="flex-start"
{...props}
/>
);
}

export default function AddressPage({ params: { principal } }: any) {
const { data: balance } = useSuspenseAccountBalance(principal, { refetchOnWindowFocus: true });
Expand All @@ -22,7 +33,7 @@ export default function AddressPage({ params: { principal } }: any) {
return (
<>
<PageTitle>Address details</PageTitle>
<Wrapper>
<AddressPageLayout>
<Stack gap={8}>
<AddressSummary
principal={principal}
Expand All @@ -38,7 +49,7 @@ export default function AddressPage({ params: { principal } }: any) {
<TokenBalanceCard address={principal} />
</Stack>
)}
</Wrapper>
</AddressPageLayout>
</>
);
}
14 changes: 0 additions & 14 deletions src/app/address/[principal]/Wrapper.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/app/getTokenPriceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LUNAR_CRUSH_API_KEY } from '../common/constants/env';
import { LunarCrushCoin } from '../common/types/lunarCrush';
import { TokenPrice } from '../common/types/tokenPrice';
import { getCacheClient } from '../common/utils/cache-client';

Expand Down
25 changes: 17 additions & 8 deletions src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NoTransactionsError } from '@/features/txs-list/utils';

Check warning on line 1 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L1

Added line #L1 was not covered by tests
import {
InfiniteData,
UseInfiniteQueryResult,
UseSuspenseInfiniteQueryResult,
useInfiniteQuery,
useSuspenseInfiniteQuery,
} from '@tanstack/react-query';
import { InfiniteData } from '@tanstack/react-query';

import {
AddressTransaction,
Expand Down Expand Up @@ -70,14 +71,22 @@ export function useSuspenseAddressTransactionInfinite(
if (!address) throw new Error('Address is required');
return useSuspenseInfiniteQuery({
queryKey: ['addressConfirmedTxsWithTransfersInfinite', address],
queryFn: ({ pageParam }: { pageParam: number }) =>
api.transactionsApi.getAddressTransactions({
address: address,
limit: DEFAULT_LIST_LIMIT,
offset: pageParam || 0,
}),
queryFn: ({ pageParam }: { pageParam: number }) => {
const response = api.transactionsApi

Check warning on line 75 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L74-L75

Added lines #L74 - L75 were not covered by tests
.getAddressTransactions({
address: address,
limit: DEFAULT_LIST_LIMIT,
offset: pageParam || 0,
})
.catch(error => {

Check warning on line 81 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L81

Added line #L81 was not covered by tests
if (error.status === 404) {
throw new NoTransactionsError('This address has no transactions.');

Check warning on line 83 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L83

Added line #L83 was not covered by tests
}
throw new Error('Failed to fetch transactions.');

Check warning on line 85 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L85

Added line #L85 was not covered by tests
});
return response;

Check warning on line 87 in src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts

View check run for this annotation

Codecov / codecov/patch

src/common/queries/useAddressConfirmedTxsWithTransfersInfinite.ts#L87

Added line #L87 was not covered by tests
},
getNextPageParam,

initialPageParam: 0,
staleTime: TWO_MINUTES,
...options,
Expand Down
1 change: 0 additions & 1 deletion src/features/txs-list/AddressMempoolTxsList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as React from 'react';
import { FC, memo } from 'react';

import { MempoolTransaction } from '@stacks/stacks-blockchain-api-types';
Expand Down
1 change: 0 additions & 1 deletion src/features/txs-list/tabs/AddressTxListTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dynamic from 'next/dynamic';
import { FC } from 'react';
import * as React from 'react';

import { ExplorerErrorBoundary } from '../../../app/_components/ErrorBoundary';
import { Section } from '../../../common/components/Section';
Expand Down
10 changes: 10 additions & 0 deletions src/features/txs-list/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NonRetryableError } from '@/app/_components/ErrorBoundary';

Check warning on line 1 in src/features/txs-list/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/features/txs-list/utils.ts#L1

Added line #L1 was not covered by tests

import { TransactionType } from '@stacks/stacks-blockchain-api-types';

export const getTransactionTypeLabel = (value: TransactionType) => {
Expand All @@ -16,3 +18,11 @@ export const getTransactionTypeLabel = (value: TransactionType) => {
return 'Tenure change';
}
};

export class NoTransactionsError extends NonRetryableError {
constructor(message: string) {
super(message);
this.name = 'No Transactions';
this.message = message;

Check warning on line 26 in src/features/txs-list/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/features/txs-list/utils.ts#L22-L26

Added lines #L22 - L26 were not covered by tests
}
}

0 comments on commit 8c974a3

Please sign in to comment.