Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/tx err reporting #4904

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/common/hooks/use-submit-stx-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { bytesToHex } from '@stacks/common';
import { StacksTransaction, broadcastTransaction } from '@stacks/transactions';

import { logger } from '@shared/logger';
import { isError } from '@shared/utils';

import { getErrorMessage } from '@app/common/get-error-message';
import { useRefreshAllAccountData } from '@app/common/hooks/account/use-refresh-all-account-data';
Expand Down Expand Up @@ -59,7 +60,7 @@ export function useSubmitTransactionCallback({ loadingKey }: UseSubmitTransactio
}
} catch (error) {
logger.error('Transaction callback', { error });
onError(error instanceof Error ? error : { name: '', message: '' });
onError(isError(error) ? error : { name: '', message: '' });
setIsIdle();
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/app/common/utils/safe-await.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TypeScript port of https://github.com/DavidWells/safe-await/
import { isError } from '@shared/utils';

// Native Error types https://mzl.la/2Veh3TR
const nativeExceptions = [
Expand All @@ -19,7 +20,7 @@ function throwNative(error: Error) {
export async function safeAwait<T>(promise: Promise<T>, finallyFn?: () => void) {
return promise
.then(data => {
if (data instanceof Error) {
if (isError(data)) {
throwNative(data);
return [data] as readonly [Error];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as yup from 'yup';
import { createMoney } from '@shared/models/money.model';
import { BitcoinTx } from '@shared/models/transactions/bitcoin-transaction.model';
import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useBtcAssetBalance } from '@app/common/hooks/balance/btc/use-btc-balance';
Expand Down Expand Up @@ -122,7 +123,7 @@ export function useBtcIncreaseFee(btcTx: BitcoinTx) {
}

function onError(error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error';
const message = isError(error) ? error.message : 'Unknown error';
toast.error(message);
navigate(RouteUrls.Home);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import get from 'lodash.get';
import { BitcoinInputSigningConfig } from '@shared/crypto/bitcoin/signer-config';
import { logger } from '@shared/logger';
import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { useLocationState, useLocationStateWithCache } from '@app/common/hooks/use-location-state';
import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
Expand Down Expand Up @@ -115,7 +116,7 @@ function LedgerSignBitcoinTxContainer() {
void bitcoinApp.transport.close();
}
} catch (e) {
if (e instanceof Error && checkLockedDeviceError(e)) {
if (isError(e) && checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import get from 'lodash.get';

import { finalizeAuthResponse } from '@shared/actions/finalize-auth-response';
import { logger } from '@shared/logger';
import { isError } from '@shared/utils';

import { useGetLegacyAuthBitcoinAddresses } from '@app/common/authentication/use-legacy-auth-bitcoin-addresses';
import { useOnboardingState } from '@app/common/hooks/auth/use-onboarding-state';
Expand Down Expand Up @@ -92,7 +93,7 @@ export function LedgerSignJwtContainer() {
const stacks = await prepareLedgerDeviceStacksAppConnection({
setLoadingState: setAwaitingDeviceConnection,
onError(e) {
if (e instanceof Error && checkLockedDeviceError(e)) {
if (isError(e) && checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import get from 'lodash.get';
import { finalizeMessageSignature } from '@shared/actions/finalize-message-signature';
import { logger } from '@shared/logger';
import { UnsignedMessage, whenSignableMessageOfType } from '@shared/signature/signature-types';
import { isError } from '@shared/utils';

import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
import { delay } from '@app/common/utils';
Expand Down Expand Up @@ -67,7 +68,7 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
const stacksApp = await prepareLedgerDeviceStacksAppConnection({
setLoadingState: setAwaitingDeviceConnection,
onError(e) {
if (e instanceof Error && checkLockedDeviceError(e)) {
if (isError(e) && checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import get from 'lodash.get';

import { logger } from '@shared/logger';
import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
import { appEvents } from '@app/common/publish-subscribe';
Expand Down Expand Up @@ -71,7 +72,7 @@ function LedgerSignStacksTxContainer() {
const stacksApp = await prepareLedgerDeviceStacksAppConnection({
setLoadingState: setAwaitingDeviceConnection,
onError(e) {
if (e instanceof Error && checkLockedDeviceError(e)) {
if (isError(e) && checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
}
Expand Down Expand Up @@ -148,7 +149,7 @@ function LedgerSignStacksTxContainer() {
signedTx,
});
} catch (e) {
ledgerNavigate.toBroadcastErrorStep(e instanceof Error ? e.message : 'Unknown error');
ledgerNavigate.toBroadcastErrorStep(isError(e) ? e.message : 'Unknown error');
return;
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import StacksApp from '@zondax/ledger-stacks';
import AppClient from 'ledger-bitcoin';

import { SupportedBlockchains } from '@shared/constants';
import { isError } from '@shared/utils';

import { delay } from '@app/common/utils';

Expand Down Expand Up @@ -73,7 +74,7 @@ export function useRequestLedgerKeys<App extends AppClient | StacksApp>({
onSuccess?.();
} catch (e) {
setAwaitingDeviceConnection(false);
if (e instanceof Error && checkLockedDeviceError(e)) {
if (isError(e) && checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/features/psbt-signer/psbt-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom';

import { getPsbtTxInputs, getPsbtTxOutputs } from '@shared/crypto/bitcoin/bitcoin.utils';
import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';
import { closeWindow, isError } from '@shared/utils';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { SignPsbtArgs } from '@app/common/psbt/requests';
Expand Down Expand Up @@ -51,7 +51,7 @@ export function PsbtSigner(props: PsbtSignerProps) {
return getRawPsbt(psbtHex);
} catch (e) {
navigate(RouteUrls.RequestError, {
state: { message: e instanceof Error ? e.message : '', title: 'Failed request' },
state: { message: isError(e) ? e.message : '', title: 'Failed request' },
});
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { StacksTransaction } from '@stacks/transactions';
import { logger } from '@shared/logger';
import { CryptoCurrencies } from '@shared/models/currencies.model';
import { RouteUrls } from '@shared/route-urls';
import { isString } from '@shared/utils';
import { isError, isString } from '@shared/utils';

import { LoadingKeys } from '@app/common/hooks/use-loading';
import { useSubmitTransactionCallback } from '@app/common/hooks/use-submit-stx-transaction';
Expand Down Expand Up @@ -56,7 +56,7 @@ export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals?
})(signedTx);
} catch (e) {
navigate(RouteUrls.TransactionBroadcastError, {
state: { message: e instanceof Error ? e.message : 'Unknown error' },
state: { message: isError(e) ? e.message : 'Unknown error' },
});
} finally {
setIsBroadcasting(false);
Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/psbt-request/use-psbt-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';

import { finalizePsbt } from '@shared/actions/finalize-psbt';
import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { usePsbtRequestSearchParams } from '@app/common/psbt/use-psbt-request-params';
Expand Down Expand Up @@ -57,7 +58,7 @@ export function usePsbtRequest() {
});
} catch (e) {
return navigate(RouteUrls.RequestError, {
state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' },
state: { message: isError(e) ? e.message : '', title: 'Failed to sign' },
});
}
},
Expand Down
8 changes: 4 additions & 4 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { bytesToHex } from '@stacks/common';
import { Money } from '@shared/models/money.model';
import { RouteUrls } from '@shared/route-urls';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';
import { closeWindow, isError } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { sumMoney } from '@app/common/money/calculate-money';
Expand Down Expand Up @@ -78,7 +78,7 @@ export function useRpcSignPsbt() {
},
onError(e) {
navigate(RouteUrls.RequestError, {
state: { message: e instanceof Error ? e.message : '', title: 'Failed to broadcast' },
state: { message: isError(e) ? e.message : '', title: 'Failed to broadcast' },
});
},
});
Expand Down Expand Up @@ -113,7 +113,7 @@ export function useRpcSignPsbt() {
} catch (e) {
return navigate(RouteUrls.RequestError, {
state: {
message: e instanceof Error ? e.message : '',
message: isError(e) ? e.message : '',
title: 'Failed to finalize tx',
},
});
Expand All @@ -130,7 +130,7 @@ export function useRpcSignPsbt() {
closeWindow();
} catch (e) {
return navigate(RouteUrls.RequestError, {
state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' },
state: { message: isError(e) ? e.message : '', title: 'Failed to sign' },
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as yup from 'yup';
import { logger } from '@shared/logger';
import { OrdinalSendFormValues } from '@shared/models/form.model';
import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { FormErrorMessages } from '@app/common/error-messages';
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
Expand Down Expand Up @@ -66,7 +67,7 @@ export function useSendInscriptionForm() {
void analytics.track('ordinals_dot_com_unavailable', { error });

let message = 'Unable to establish if utxo has multiple inscriptions';
if (error instanceof Error) {
if (isError(error)) {
message = error.message;
}
setShowError(message);
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StacksTransaction } from '@stacks/transactions';

import { logger } from '@shared/logger';
import { RouteUrls } from '@shared/route-urls';
import { isString } from '@shared/utils';
import { isError, isString } from '@shared/utils';

import { LoadingKeys, useLoading } from '@app/common/hooks/use-loading';
import { useSubmitTransactionCallback } from '@app/common/hooks/use-submit-stx-transaction';
Expand Down Expand Up @@ -42,7 +42,7 @@ export function useStacksBroadcastSwap() {
} catch (e) {
setIsIdle();
navigate(RouteUrls.TransactionBroadcastError, {
state: { message: e instanceof Error ? e.message : 'Unknown error' },
state: { message: isError(e) ? e.message : 'Unknown error' },
});
} finally {
setIsIdle();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useCallback, useState } from 'react';

import { isError } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { delay } from '@app/common/utils';
import { useBitcoinClient } from '@app/store/common/api-clients.hooks';
Expand Down Expand Up @@ -30,7 +32,10 @@ export function useBitcoinBroadcastTransaction() {
return txid;
} catch (e) {
onError?.(e as Error);
void analytics.track('error_broadcasting_transaction', { error: e });
void analytics.track('error_broadcasting_transaction', {
errorName: isError(e) ? e.name : 'unknown',
errorMsg: isError(e) ? e.message : 'unknown',
});
return;
} finally {
setIsBroadcasting(false);
Expand Down
4 changes: 4 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function isObject(value: unknown): value is object {
return typeof value === 'object';
}

export function isError(value: unknown): value is Error {
return value instanceof Error;
}

export function isEmpty(value: object) {
return Object.keys(value).length === 0;
}
Expand Down
Loading