Skip to content

Currency conversion results in zero amount causing service failures #104

@osw282

Description

@osw282

Issue Diagnosis

I've identified a critical issue in the currencyservice that's causing conversion failures and service degradation.

Problem Description

The logs show repeated occurrences of the following error:

"Currency conversion resulted in zero amount, switching back to original currency"

Root Cause Analysis

Looking at the server.js file (lines 156-177), the conversion logic has a flawed approach:

  1. When a currency conversion results in zero (both units and nanos are 0), the service falls back to the original currency
  2. This fallback approach indicates underlying issues with the currency conversion rates or mathematical precision
  3. The zero-amount conversions are likely caused by:
    • Division by very large exchange rates causing precision loss
    • Improper handling of fractional amounts in the _carry function
    • Potential issues with the currency_conversion.json data

Technical Details

The problematic code section:

// Check if the result is zero
if (result.units === 0 && result.nanos === 0) {
  logger.error(`Currency conversion resulted in zero amount, switching back to original currency`);
  notifySlack();
  
  // Switch back to the original currency
  result.currency_code = request.from.currency_code;
  result.units = request.from.units;
  result.nanos = request.from.nanos;
  
  callback(null, result);
  return;
}

Recommended Fix

  1. Improve precision handling: Modify the conversion logic to use higher precision arithmetic or libraries like decimal.js
  2. Add validation: Check for valid conversion rates before performing calculations
  3. Enhance error handling: Instead of silently falling back, throw appropriate errors for invalid conversions
  4. Review currency data: Audit the currency_conversion.json file for accuracy

Immediate fix suggestion:

// Add minimum threshold check instead of exact zero
const MINIMUM_AMOUNT = 0.001; // or appropriate threshold
if (result.units === 0 && result.nanos < MINIMUM_AMOUNT * Math.pow(10, 9)) {
  // Log detailed conversion info for debugging
  logger.error(`Currency conversion below minimum threshold: ${from.currency_code} to ${request.to_code}, rate: ${data[request.to_code]}`);
  // Return error instead of fallback
  callback(new Error('Currency conversion resulted in amount below minimum threshold'));
  return;
}

Impact

  • Service reliability issues
  • Inconsistent currency conversion behavior
  • User experience degradation
  • Increased Slack notifications creating noise

This issue requires immediate attention to maintain service quality and user trust.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions