Throw errors for invalid currency conversion inputs - #1381
pavankumar-vh wants to merge 1 commit into
Conversation
The previous version silently returned 0 for invalid inputs, which could mask serious bugs in money-conversion paths. Changed to throw explicit errors with descriptive messages so invalid inputs are caught immediately during development and testing. - Fixed the validation to use Number.isFinite() instead of relying on comparison operators that don't catch NaN - Changed from returning 0 to throwing errors for invalid inputs - Added comprehensive test coverage for all error paths and happy paths All 8 tests pass.
|
Good instinct: silently returning 0 on invalid currency inputs is a real risk, and the added test coverage in The concern is the change in behavior at call sites. The PR body says "no external call sites found," but these are money-conversion functions almost certainly invoked from billing/Stripe webhook or grant-processing code elsewhere in the private tree (not visible in this mirror). Converting silent-zero to a thrown exception is the right long-term fix, but it needs to be paired with call-site handling - e.g. wrapping the Stripe webhook handler in a try/catch that logs and alerts, rather than letting an uncaught exception crash a webhook handler or background job. Right now this is a pure library change with no visibility into how callers will react to a new thrown error, which is risky for a payments path. Also minor: Worth porting in spirit, but a maintainer will want to confirm/adjust the calling code before taking this as-is, since it changes a shared utility's contract without touching the callers that depend on it not throwing. |
Overview
Fix currency conversion functions in
common/src/util/currency.tsto throw explicit errors for invalid inputs instead of silently returning 0.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:
All 8 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.