Skip to content

Commit 969621e

Browse files
authored
chore(checkout-widgets): Track errors (#2681)
1 parent cce02d6 commit 969621e

File tree

9 files changed

+34
-17
lines changed

9 files changed

+34
-17
lines changed

packages/checkout/widgets-lib/src/lib/hooks/useSignOrder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
SignCurrencyFilter,
2424
SquidPostHookCall,
2525
} from '../primary-sales';
26-
import { filterAllowedTransactions, hexToText } from '../utils';
26+
import { errorToString, filterAllowedTransactions, hexToText } from '../utils';
2727

2828
const toSignedProduct = (
2929
product: SignApiProduct,
@@ -194,7 +194,7 @@ export const useSignOrder = (input: SignOrderInput) => {
194194
}
195195
const error: SignOrderError = {
196196
type: errorType,
197-
data: { error: err },
197+
data: { error: errorToString(err) },
198198
};
199199
setSignError(error);
200200
return [undefined, error];

packages/checkout/widgets-lib/src/lib/primary-sales.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type SignOrderInput = {
6161

6262
export type SignOrderError = {
6363
type: SaleErrorTypes;
64-
data?: Record<string, unknown>;
64+
data?: Record<string, string>;
6565
};
6666

6767
export type ExecutedTransaction = {

packages/checkout/widgets-lib/src/lib/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ export function removeSpace(str: string): string {
240240
return str.replace(/\s/g, '');
241241
}
242242

243+
export const errorToString = (error: unknown) => {
244+
if (error instanceof Error) {
245+
return error.message;
246+
}
247+
if (typeof error === 'string') {
248+
return error;
249+
}
250+
251+
return JSON.stringify(error);
252+
};
253+
243254
export const filterAllowedTransactions = async (
244255
transactions: SignedTransaction[],
245256
provider: WrappedBrowserProvider,

packages/checkout/widgets-lib/src/widgets/sale/context/SaleContextProvider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
useState,
1717
} from 'react';
1818
import { Environment } from '@imtbl/config';
19+
import { trackError } from '@imtbl/metrics';
1920
import { ConnectLoaderState } from '../../../context/connect-loader-context/ConnectLoaderContext';
2021
import { SaleWidgetViews } from '../../../context/view-context/SaleViewContextTypes';
2122
import {
@@ -88,7 +89,7 @@ type SaleContextValues = SaleContextProps & {
8889
paymentMethod?: SalePaymentTypes | undefined,
8990
data?: Record<string, unknown>
9091
) => void;
91-
goToErrorView: (type: SaleErrorTypes, data?: Record<string, unknown>) => void;
92+
goToErrorView: (type: SaleErrorTypes, data?: Record<string, string>) => void;
9293
goToSuccessView: (data?: Record<string, unknown>) => void;
9394
fundingRoutes: FundingRoute[];
9495
disabledPaymentTypes: SalePaymentTypes[];
@@ -306,13 +307,15 @@ export function SaleContextProvider(props: {
306307
);
307308

308309
const goToErrorView = useCallback(
309-
(errorType: SaleErrorTypes, data: Record<string, unknown> = {}) => {
310+
(errorType: SaleErrorTypes, data: Record<string, string> = {}) => {
310311
errorRetries.current += 1;
311312
if (errorRetries.current > MAX_ERROR_RETRIES) {
312313
errorRetries.current = 0;
313314
setPaymentMethod(undefined);
314315
}
315316

317+
trackError('commerce', 'saleError', new Error(errorType), data);
318+
316319
viewDispatch({
317320
payload: {
318321
type: ViewActions.UPDATE_VIEW,

packages/checkout/widgets-lib/src/widgets/sale/hooks/useQuoteOrder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect, useRef } from 'react';
22
import { Environment } from '@imtbl/config';
33
import { WrappedBrowserProvider, SaleItem } from '@imtbl/checkout-sdk';
4-
import { compareStr } from '../../../lib/utils';
4+
import { compareStr, errorToString } from '../../../lib/utils';
55
import { PRIMARY_SALES_API_BASE_URL } from '../utils/config';
66

77
import { OrderQuote, OrderQuoteCurrency, SaleErrorTypes } from '../types';
@@ -26,7 +26,7 @@ export const defaultOrderQuote: OrderQuote = {
2626

2727
export type ConfigError = {
2828
type: SaleErrorTypes;
29-
data?: Record<string, unknown>;
29+
data?: Record<string, string>;
3030
};
3131

3232
export const useQuoteOrder = ({
@@ -46,7 +46,7 @@ export const useQuoteOrder = ({
4646
ConfigError | undefined
4747
>(undefined);
4848

49-
const setError = (error: unknown) => {
49+
const setError = (error: string) => {
5050
setOrderQuoteError({
5151
type: SaleErrorTypes.SERVICE_BREAKDOWN,
5252
data: { reason: 'Error fetching settlement currencies', error },
@@ -69,7 +69,7 @@ export const useQuoteOrder = ({
6969

7070
setQueryParams(params.toString());
7171
} catch (error) {
72-
setError(error);
72+
setError(errorToString(error));
7373
}
7474
})();
7575
}, [items, provider]);
@@ -102,7 +102,7 @@ export const useQuoteOrder = ({
102102
);
103103
setOrderQuote(config);
104104
} catch (error) {
105-
setError(error);
105+
setError(errorToString(error));
106106
} finally {
107107
fetching.current = false;
108108
}

packages/checkout/widgets-lib/src/widgets/sale/hooks/useSignOrder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { PRIMARY_SALES_API_BASE_URL } from '../utils/config';
1818
import { hexToText } from '../functions/utils';
1919
import { filterAllowedTransactions } from '../functions/signUtils';
20+
import { errorToString } from '../../../lib/utils';
2021

2122
type SignApiTransaction = {
2223
contract_address: string;
@@ -249,7 +250,7 @@ export const useSignOrder = (input: SignOrderInput) => {
249250
}
250251
const error: SignOrderError = {
251252
type: errorType,
252-
data: { error: err },
253+
data: { error: errorToString(err) },
253254
};
254255
setSignError(error);
255256
return [undefined, error];

packages/checkout/widgets-lib/src/widgets/sale/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type SignOrderInput = {
5959

6060
export type SignOrderError = {
6161
type: SaleErrorTypes;
62-
data?: Record<string, unknown>;
62+
data?: Record<string, string>;
6363
};
6464

6565
export type ExecutedTransaction = {

packages/checkout/widgets-lib/src/widgets/sale/views/OrderSummary.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
import { HandoverTarget } from '../../../context/handover-context/HandoverContext';
3333
import { ViewContext, ViewActions, SharedViews } from '../../../context/view-context/ViewContext';
3434
import { useHandover } from '../../../lib/hooks/useHandover';
35-
import { getRemoteRive } from '../../../lib/utils';
35+
import { errorToString, getRemoteRive } from '../../../lib/utils';
3636

3737
type OrderSummaryProps = {
3838
subView: OrderSummarySubViews;
@@ -202,8 +202,10 @@ export function OrderSummary({ subView }: OrderSummaryProps) {
202202
},
203203
},
204204
});
205-
} catch (error: any) {
206-
goToErrorView(SaleErrorTypes.SERVICE_BREAKDOWN, error);
205+
} catch (error: unknown) {
206+
goToErrorView(SaleErrorTypes.SERVICE_BREAKDOWN, {
207+
error: errorToString(error),
208+
});
207209
}
208210
}, [fundingBalances, loadingBalances, fundingBalancesResult]);
209211

packages/checkout/widgets-lib/src/widgets/sale/views/PayWithCoins.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { HandoverTarget } from '../../../context/handover-context/HandoverContex
1616
import { HandoverContent } from '../../../components/Handover/HandoverContent';
1717
import { SaleWidgetViews } from '../../../context/view-context/SaleViewContextTypes';
1818
import { isPassportProvider } from '../../../lib/provider';
19-
import { getRemoteRive } from '../../../lib/utils';
19+
import { errorToString, getRemoteRive } from '../../../lib/utils';
2020
import { HandoverDuration } from '../../../context/handover-context/HandoverProvider';
2121

2222
interface StepConfig {
@@ -120,7 +120,7 @@ export function PayWithCoins() {
120120
onTxnStepExecuteNextTransaction,
121121
);
122122
} catch (error) {
123-
goToErrorView(SaleErrorTypes.SERVICE_BREAKDOWN, { error });
123+
goToErrorView(SaleErrorTypes.SERVICE_BREAKDOWN, { error: errorToString(error) });
124124
}
125125
};
126126

0 commit comments

Comments
 (0)