From 992d826179dc58cb1df6ea0bc04e61ea6390cc92 Mon Sep 17 00:00:00 2001 From: Parth <143504541+parrth20@users.noreply.github.com> Date: Wed, 18 Feb 2026 17:50:37 +0530 Subject: [PATCH] refactor(sweep): use geterrorreason and clarify schedule index naming (#1122) * refactor(sweep): use getErrorReason in sweep error paths * refactor(sweep): rename currentTransaction to currentTransactionIndex * fix(sweep): keep runschedule mutation error typing compatible --------- Co-authored-by: Saurabh Singh --- src/components/sweep/SweepPage.tsx | 21 +++++-------------- .../sweep/SweepScheduleProgress.tsx | 2 +- src/components/sweep/scheduleUtils.test.ts | 2 +- src/components/sweep/scheduleUtils.ts | 6 +++--- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/components/sweep/SweepPage.tsx b/src/components/sweep/SweepPage.tsx index 29495da7..c5957256 100644 --- a/src/components/sweep/SweepPage.tsx +++ b/src/components/sweep/SweepPage.tsx @@ -25,6 +25,7 @@ import { useJamWalletInfoContext } from '@/context/JamWalletInfoContext' import { useApiClient } from '@/hooks/useApiClient' import { useFeeConfigValidation } from '@/hooks/useFeeConfigValidation' import { useRefreshSession } from '@/hooks/useRefreshSession' +import { getErrorReason } from '@/lib/errorReason' import type { WalletFileName } from '@/lib/utils' import { jmSessionStore } from '@/store/jmSessionStore' import { Spinner } from '../ui/spinner' @@ -40,18 +41,6 @@ const WAIT_FOR_UPDATE_SESSION_POLLING_DELAY = 1_000 const initialDestinationAddresses = () => Array.from({ length: DESTINATION_ADDRESS_COUNT }, () => '') const initialTouchedValues = () => Array.from({ length: DESTINATION_ADDRESS_COUNT }, () => false) -const toErrorReason = (error: unknown, fallback: string): string => { - if (typeof error === 'object' && error !== null) { - if ('message' in error && typeof error.message === 'string' && error.message.trim() !== '') { - return error.message - } - if ('error_description' in error && typeof error.error_description === 'string' && error.error_description !== '') { - return error.error_description - } - } - return fallback -} - export const SweepPage = ({ walletFileName }: SweepPageProps) => { const { t } = useTranslation() const client = useApiClient() @@ -103,7 +92,7 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => { if (result.response.status === 404) { return null } - throw new Error(toErrorReason(result.error, 'Failed to load schedule')) + throw new Error(getErrorReason(result.error, 'Failed to load schedule')) } return result.data @@ -136,8 +125,7 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => { setShowScheduleConfirmDialog(false) }, onError: (error: ErrorMessage) => { - const reason = error.message || error.error_description || t('global.errors.reason_unknown') - // TODO: i18n add reason to message + const reason = getErrorReason(error, t('global.errors.reason_unknown')) const message = `${t('scheduler.error_starting_schedule_failed')} ${reason}` setAlertMessage(message) toast.error(message) @@ -156,7 +144,8 @@ export const SweepPage = ({ walletFileName }: SweepPageProps) => { setLocalSchedule(undefined) }, onError: (error: unknown) => { - const message = `${t('scheduler.error_stopping_schedule_failed')} ${toErrorReason(error, t('global.errors.reason_unknown'))}` + const reason = getErrorReason(error, t('global.errors.reason_unknown')) + const message = `${t('scheduler.error_stopping_schedule_failed')} ${reason}` setAlertMessage(message) toast.error(message) }, diff --git a/src/components/sweep/SweepScheduleProgress.tsx b/src/components/sweep/SweepScheduleProgress.tsx index 1d0cd89a..73ea7aaa 100644 --- a/src/components/sweep/SweepScheduleProgress.tsx +++ b/src/components/sweep/SweepScheduleProgress.tsx @@ -81,7 +81,7 @@ export const SweepScheduleProgress = ({ schedule, isStopping, onStop }: SweepSch { expect(summary.totalTransactions).toBe(3) expect(summary.completedTransactions).toBe(1) - expect(summary.currentTransaction).toBe(2) + expect(summary.currentTransactionIndex).toBe(1) expect(summary.isDone).toBe(false) expect(summary.steps).toHaveLength(3) expect(summary.steps[1].isActive).toBe(true) diff --git a/src/components/sweep/scheduleUtils.ts b/src/components/sweep/scheduleUtils.ts index a34f5717..70039058 100644 --- a/src/components/sweep/scheduleUtils.ts +++ b/src/components/sweep/scheduleUtils.ts @@ -13,7 +13,7 @@ export interface ScheduleProgressSummary { totalWaitSeconds: number totalTransactions: number completedTransactions: number - currentTransaction: number + currentTransactionIndex: number isDone: boolean steps: ScheduleProgressStep[] } @@ -51,7 +51,7 @@ export const toScheduleProgressSummary = (schedule: Schedule): ScheduleProgressS totalWaitSeconds: 0, totalTransactions: 0, completedTransactions: 0, - currentTransaction: 0, + currentTransactionIndex: 0, isDone: true, steps: [], } @@ -92,7 +92,7 @@ export const toScheduleProgressSummary = (schedule: Schedule): ScheduleProgressS totalWaitSeconds, totalTransactions: schedule.length, completedTransactions: Math.min(completedTransactions, schedule.length), - currentTransaction: Math.min(completedTransactions + 1, schedule.length), + currentTransactionIndex: Math.min(completedTransactions, schedule.length - 1), isDone, steps, }