From 0874af69e9cf1a283b121bea5e950a8af21652d6 Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Fri, 4 Sep 2026 00:48:28 +0530 Subject: [PATCH] Throw error for invalid currency conversion inputs The previous version silently returned 0 when centsPerCredit was invalid or credits/amountInCents was NaN/Infinity. This is dangerous in money-conversion paths because misconfigurations would silently produce wrong values rather than failing loudly. Changed to throw explicit errors with descriptive messages so invalid inputs are caught immediately during development and testing. --- common/src/util/currency.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/common/src/util/currency.ts b/common/src/util/currency.ts index b3499c01e5..ccdbc5cca6 100644 --- a/common/src/util/currency.ts +++ b/common/src/util/currency.ts @@ -3,11 +3,22 @@ * @param credits The number of credits to convert * @param centsPerCredit The cost per credit in cents * @returns The amount in USD cents + * @throws Error if centsPerCredit is not positive or credits is not finite */ export function convertCreditsToUsdCents( credits: number, centsPerCredit: number, ): number { + if (!(centsPerCredit > 0)) { + throw new Error( + `convertCreditsToUsdCents: centsPerCredit must be positive, got ${centsPerCredit}`, + ) + } + if (!Number.isFinite(credits)) { + throw new Error( + `convertCreditsToUsdCents: credits must be finite, got ${credits}`, + ) + } return Math.ceil(credits * centsPerCredit) } @@ -16,10 +27,21 @@ export function convertCreditsToUsdCents( * @param amountInCents The amount in USD cents * @param centsPerCredit The cost per credit in cents * @returns The number of credits + * @throws Error if centsPerCredit is not positive or amountInCents is not finite */ export function convertStripeGrantAmountToCredits( amountInCents: number, centsPerCredit: number, ): number { + if (!(centsPerCredit > 0)) { + throw new Error( + `convertStripeGrantAmountToCredits: centsPerCredit must be positive, got ${centsPerCredit}`, + ) + } + if (!Number.isFinite(amountInCents)) { + throw new Error( + `convertStripeGrantAmountToCredits: amountInCents must be finite, got ${amountInCents}`, + ) + } return Math.floor(amountInCents / centsPerCredit) }