diff --git a/libs/wallet/src/use-vega-transaction-manager.spec.tsx b/libs/wallet/src/use-vega-transaction-manager.spec.tsx index 7cef04484..62acefd4e 100644 --- a/libs/wallet/src/use-vega-transaction-manager.spec.tsx +++ b/libs/wallet/src/use-vega-transaction-manager.spec.tsx @@ -13,10 +13,12 @@ const mockSendTx = jest.fn | null>, []>(); const pubKey = 'pubKey'; +const mockDisconnect = jest.fn(); jest.mock('./use-vega-wallet', () => ({ useVegaWallet: () => ({ sendTx: mockSendTx, pubKey, + disconnect: mockDisconnect, }), })); @@ -104,6 +106,17 @@ describe('useVegaTransactionManager', () => { mockSendTx.mockRejectedValue(null); renderHook(useVegaTransactionManager); await waitForNextTick(); + expect(mockDisconnect).not.toHaveBeenCalledWith(); + expect(update).toBeCalled(); + expect(update.mock.calls[0][1]?.status).toEqual(VegaTxStatus.Error); + }); + + it('call disconnect if detect no service error', async () => { + mockTransactionStoreState.mockReturnValue(defaultState); + mockSendTx.mockRejectedValue(new TypeError('Failed to fetch')); + renderHook(useVegaTransactionManager); + await waitForNextTick(); + expect(mockDisconnect).toHaveBeenCalledWith(); expect(update).toBeCalled(); expect(update.mock.calls[0][1]?.status).toEqual(VegaTxStatus.Error); }); diff --git a/libs/wallet/src/use-vega-transaction-manager.tsx b/libs/wallet/src/use-vega-transaction-manager.tsx index c8a3073f3..6b4771a07 100644 --- a/libs/wallet/src/use-vega-transaction-manager.tsx +++ b/libs/wallet/src/use-vega-transaction-manager.tsx @@ -1,12 +1,9 @@ import { useVegaWallet } from './use-vega-wallet'; import { useEffect, useRef } from 'react'; +import type { WalletError } from './connectors'; import { ClientErrors } from './connectors'; -import { VegaTxStatus } from './use-vega-transaction'; +import { VegaTxStatus, orderErrorResolve } from './use-vega-transaction'; import { useVegaTransactionStore } from './use-vega-transaction-store'; -import { - WalletClientError, - WalletHttpError, -} from '@vegaprotocol/wallet-client'; export const useVegaTransactionManager = () => { const { sendTx, pubKey, disconnect } = useVegaWallet(); @@ -41,13 +38,8 @@ export const useVegaTransactionManager = () => { } }) .catch((err) => { - const error = - err instanceof WalletClientError - ? err - : err instanceof WalletHttpError - ? ClientErrors.UNKNOWN - : ClientErrors.NO_SERVICE; - if (error.code === ClientErrors.NO_SERVICE.code) { + const error = orderErrorResolve(err); + if ((error as WalletError).code === ClientErrors.NO_SERVICE.code) { disconnect(); } update(transaction.id, { diff --git a/libs/wallet/src/use-vega-transaction.spec.tsx b/libs/wallet/src/use-vega-transaction.spec.tsx index 7e804e074..14ee15f03 100644 --- a/libs/wallet/src/use-vega-transaction.spec.tsx +++ b/libs/wallet/src/use-vega-transaction.spec.tsx @@ -87,7 +87,7 @@ describe('useVegaTransaction', () => { ); }); - it('handles an unkwown error', () => { + it('handles an unknown error', () => { const unknownThrow = { foo: 'bar' }; const mockSendTx = jest.fn(() => { throw unknownThrow; diff --git a/libs/wallet/src/use-vega-transaction.tsx b/libs/wallet/src/use-vega-transaction.tsx index 2cf546383..0256e25e7 100644 --- a/libs/wallet/src/use-vega-transaction.tsx +++ b/libs/wallet/src/use-vega-transaction.tsx @@ -9,6 +9,7 @@ import type { VegaTransactionContentMap } from './vega-transaction-dialog'; import { VegaTransactionDialog } from './vega-transaction-dialog'; import type { Intent } from '@vegaprotocol/ui-toolkit'; import type { Transaction } from './connectors'; +import type { WalletError } from './connectors'; import { ClientErrors } from './connectors'; export interface DialogProps { @@ -42,6 +43,19 @@ export const initialState = { dialogOpen: false, }; +export const orderErrorResolve = (err: Error | unknown): Error => { + if (err instanceof WalletClientError) { + return err; + } else if (err instanceof WalletHttpError) { + return ClientErrors.UNKNOWN; + } else if (err instanceof TypeError) { + return ClientErrors.NO_SERVICE; + } else if (err instanceof Error) { + return err; + } + return ClientErrors.UNKNOWN; +}; + export const useVegaTransaction = () => { const { sendTx, disconnect } = useVegaWallet(); const [transaction, _setTransaction] = useState(initialState); @@ -92,13 +106,8 @@ export const useVegaTransaction = () => { return null; } catch (err) { - const error = - err instanceof WalletClientError - ? err - : err instanceof WalletHttpError - ? ClientErrors.UNKNOWN - : ClientErrors.NO_SERVICE; - if (error.code === ClientErrors.NO_SERVICE.code) { + const error = orderErrorResolve(err); + if ((error as WalletError).code === ClientErrors.NO_SERVICE.code) { disconnect(); } setTransaction({