Skip to content

Throw error for invalid currency conversion inputs - #1262

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/currency-throw-on-invalid
Open

Throw error for invalid currency conversion inputs#1262
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/currency-throw-on-invalid

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix currency conversion functions in common/src/util/currency.ts to throw errors instead of silently returning 0 for invalid inputs.

Bug Description

The previous version silently returned 0 when:

  • centsPerCredit was 0 or negative (division by zero or negative rate)
  • credits or amountInCents was NaN or Infinity

This 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:

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}`,
  )
}

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 inputs

Scope

This change only touches common/ which is an approved contribution area per the Contributing Guide.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant