Skip to content

Commit

Permalink
chore: more error cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 14, 2024
1 parent b893919 commit 7eb02d5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
37 changes: 26 additions & 11 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useSwitchChain,
useWaitForTransactionReceipt,
} from 'wagmi';
import { METHOD_NOT_SUPPORTED_ERROR_SUBSTRING } from '../constants';
import { useCallsStatus } from '../hooks/useCallsStatus';
import { useWriteContract } from '../hooks/useWriteContract';
import { useWriteContracts } from '../hooks/useWriteContracts';
Expand Down Expand Up @@ -235,16 +236,13 @@ describe('TransactionProvider', () => {
statusWriteContracts: 'IDLE',
writeContractsAsync: writeContractsAsyncMock,
});

render(
<TransactionProvider address="0x123" contracts={[]}>
<TestComponent />
</TransactionProvider>,
);

const button = screen.getByText('Submit');
fireEvent.click(button);

await waitFor(() => {
const errorMessage = screen.getByTestId('context-value-errorMessage');
expect(errorMessage.textContent).toBe('Request denied.');
Expand All @@ -259,7 +257,6 @@ describe('TransactionProvider', () => {
(useCallsStatus as ReturnType<typeof vi.fn>).mockReturnValue({
transactionHash: 'hash',
});

render(
<TransactionProvider
address="0x123"
Expand All @@ -269,7 +266,6 @@ describe('TransactionProvider', () => {
<TestComponent />
</TransactionProvider>,
);

await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: [{ status: 'success' }],
Expand All @@ -283,21 +279,43 @@ describe('TransactionProvider', () => {
switchChainAsync: switchChainAsyncMock,
});
(useAccount as ReturnType<typeof vi.fn>).mockReturnValue({ chainId: 1 });

render(
<TransactionProvider address="0x123" chainId={2} contracts={[]}>
<TestComponent />
</TransactionProvider>,
);

const button = screen.getByText('Submit');
fireEvent.click(button);

await waitFor(() => {
expect(switchChainAsyncMock).toHaveBeenCalledWith({ chainId: 2 });
});
});

it('should call fallbackToWriteContract when executeContracts fails', async () => {
const writeContractsAsyncMock = vi
.fn()
.mockRejectedValue(new Error(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING));
const writeContractAsyncMock = vi.fn();
(useWriteContracts as ReturnType<typeof vi.fn>).mockReturnValue({
statusWriteContracts: 'IDLE',
writeContractsAsync: writeContractsAsyncMock,
});
(useWriteContract as ReturnType<typeof vi.fn>).mockReturnValue({
status: 'IDLE',
writeContractAsync: writeContractAsyncMock,
});
render(
<TransactionProvider address="0x123" contracts={[{}]}>
<TestComponent />
</TransactionProvider>,
);
const button = screen.getByText('Submit');
fireEvent.click(button);
await waitFor(() => {
expect(writeContractAsyncMock).toHaveBeenCalled();
});
});

it('should handle generic error during fallback', async () => {
const writeContractsAsyncMock = vi
.fn()
Expand All @@ -313,16 +331,13 @@ describe('TransactionProvider', () => {
status: 'IDLE',
writeContractAsync: writeContractAsyncMock,
});

render(
<TransactionProvider address="0x123" contracts={[{}]}>
<TestComponent />
</TransactionProvider>,
);

const button = screen.getByText('Submit');
fireEvent.click(button);

await waitFor(() => {
expect(screen.getByTestId('context-value-errorMessage').textContent).toBe(
'Something went wrong. Please try again.',
Expand Down
65 changes: 36 additions & 29 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ export function TransactionProvider({
});
receipts.push(txnReceipt);
} catch (err) {
console.error('getTransactionReceiptsError', err);
setErrorMessage(GENERIC_ERROR_MESSAGE);
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'TmTPc01', // Transaction module TransactionProvider component 01 error
error: JSON.stringify(err),
message: GENERIC_ERROR_MESSAGE,
},
});
}
}
setReceiptArray(receipts);
Expand All @@ -144,7 +150,14 @@ export function TransactionProvider({
const errorMessage = isUserRejectedRequestError(err)
? 'Request denied.'
: GENERIC_ERROR_MESSAGE;
setErrorMessage(errorMessage);
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'TmTPc02', // Transaction module TransactionProvider component 02 error
error: JSON.stringify(err),
message: errorMessage,
},
});
}
}
}, [contracts, writeContractAsync]);
Expand All @@ -165,40 +178,34 @@ export function TransactionProvider({
});
}, [writeContractsAsync, contracts, capabilities]);

const handleSubmitErrors = useCallback(
async (err: unknown) => {
// handles EOA writeContracts error
// (fallback to writeContract)
if (
err instanceof Error &&
err.message.includes(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING)
) {
try {
await fallbackToWriteContract();
} catch (_err) {
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
// handles user rejected request error
} else if (isUserRejectedRequestError(err)) {
setErrorMessage('Request denied.');
// handles generic error
} else {
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
},
[fallbackToWriteContract],
);

const handleSubmit = useCallback(async () => {
setErrorMessage('');
setIsToastVisible(true);
try {
await switchChain(chainId);
await executeContracts();
} catch (err) {
await handleSubmitErrors(err);
// handles EOA writeContracts error (fallback to writeContract)
if (
err instanceof Error &&
err.message.includes(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING)
) {
await fallbackToWriteContract();
return;
}
const errorMessage = isUserRejectedRequestError(err)
? 'Request denied.'
: GENERIC_ERROR_MESSAGE;
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'TmTPc03', // Transaction module TransactionProvider component 03 error
error: JSON.stringify(err),
message: errorMessage,
},
});
}
}, [chainId, executeContracts, handleSubmitErrors, switchChain]);
}, [chainId, executeContracts, fallbackToWriteContract, switchChain]);

useEffect(() => {
if (receiptArray?.length) {
Expand Down
Empty file.

0 comments on commit 7eb02d5

Please sign in to comment.