Throw error for invalid currency conversion inputs - #1262
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.
|
Good instinct — silently returning 0 for a misconfigured A few things to fix before this is portable:
Worth resubmitting with tests and a caller audit — the core idea is sound. |
Overview
Fix currency conversion functions in
common/src/util/currency.tsto throw errors instead of silently returning 0 for invalid inputs.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
No existing tests for these functions, but the fix prevents silent failures in production.
Files Changed
common/src/util/currency.ts- Throw errors for invalid inputsScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.