Throw error for invalid currency conversion inputs with tests - #1311
Throw error for invalid currency conversion inputs with tests#1311pavankumar-vh wants to merge 1 commit into
Conversation
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. Added comprehensive test coverage for: - Normal conversion cases - Non-positive centsPerCredit (zero, negative) - NaN inputs (credits, amountInCents) - Infinity inputs (positive and negative) All 10 tests pass.
|
Thanks for adding tests alongside the fix — that's the right instinct and the test file is clean and readable. The core issue: these functions live in Substantively, throwing on non-finite/non-positive inputs is a defensible improvement over silently returning 0 — I'd lean toward porting the validation logic itself. But:
Worth a maintainer's attention to evaluate against the real call sites, but not a drop-in port as-is — needs-work rather than a straightforward port-candidate. |
Overview
Fix currency conversion functions in
common/src/util/currency.tsto throw errors instead of silently returning 0 for invalid inputs, with comprehensive test coverage.Bug Description
The previous version silently returned 0 when:
centsPerCreditwas 0 or negative (division by zero or negative rate)creditsoramountInCentswas NaN or InfinityThis is dangerous in money-conversion paths because misconfigurations would silently produce wrong values rather than failing loudly. A bad Stripe price setup or calculation error would result in users getting 0 credits/cents without any indication that something went wrong.
Fix
Changed to throw explicit errors with descriptive messages so invalid inputs are caught immediately during development and testing.
Testing
Added comprehensive test coverage for:
All 10 tests pass.
Call Site Audit
Searched for call sites of both functions:
convertCreditsToUsdCents: No external call sites found in codebase (only used internally)convertStripeGrantAmountToCredits: No external call sites found in codebase (only used internally)Both functions appear to be used internally within the codebase, and throwing errors will help catch configuration issues early.
Files Changed
common/src/util/currency.ts- Added validation and error throwingcommon/src/util/__tests__/currency.test.ts- Added comprehensive test coverageScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.