{"record":{"id":"9b16917dff39e372","repo":"nopSolutions/nopCommerce","slug":"exchange-rate-not-found-for-currency-sourcecurre","errorCode":null,"errorMessage":"Exchange rate not found for currency [{sourceCurrencyCode.Name}]","messagePattern":"Exchange rate not found for currency \\[(.+?)\\]","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Directory/CurrencyService.cs","lineNumber":253,"sourceCode":"    /// <param name=\"amount\">Amount</param>\r\n    /// <param name=\"sourceCurrencyCode\">Source currency code</param>\r\n    /// <returns>\r\n    /// A task that represents the asynchronous operation\r\n    /// The task result contains the converted value\r\n    /// </returns>\r\n    public virtual async Task<decimal> ConvertToPrimaryExchangeRateCurrencyAsync(decimal amount, Currency sourceCurrencyCode)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(sourceCurrencyCode);\r\n\r\n        var primaryExchangeRateCurrency = await GetCurrencyByIdAsync(_currencySettings.PrimaryExchangeRateCurrencyId) ?? throw new Exception(\"Primary exchange rate currency cannot be loaded\");\r\n\r\n        var result = amount;\r\n        if (result == decimal.Zero || sourceCurrencyCode.Id == primaryExchangeRateCurrency.Id)\r\n            return result;\r\n\r\n        var exchangeRate = sourceCurrencyCode.Rate;\r\n        if (exchangeRate == decimal.Zero)\r\n            throw new NopException($\"Exchange rate not found for currency [{sourceCurrencyCode.Name}]\");\r\n        result /= exchangeRate;\r\n\r\n        return result;\r\n    }\r\n\r\n    /// <summary>\r\n    /// Converts from primary exchange rate currency\r\n    /// </summary>\r\n    /// <param name=\"amount\">Amount</param>\r\n    /// <param name=\"targetCurrencyCode\">Target currency code</param>\r\n    /// <returns>\r\n    /// A task that represents the asynchronous operation\r\n    /// The task result contains the converted value\r\n    /// </returns>\r\n    public virtual async Task<decimal> ConvertFromPrimaryExchangeRateCurrencyAsync(decimal amount, Currency targetCurrencyCode)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(targetCurrencyCode);\r\n\r","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Directory/CurrencyService.cs#L235-L271","documentation":"Thrown by ConvertToPrimaryExchangeRateCurrencyAsync as a NopException when sourceCurrencyCode.Rate equals decimal.Zero. The conversion divides by the source currency's Rate, so a zero rate would either divide-by-zero or yield nonsense; the guard rejects it explicitly.","triggerScenarios":"Calling ConvertToPrimaryExchangeRateCurrencyAsync with a Currency whose Rate property is 0 (unset). The branch only triggers when the source differs from the primary currency and amount is non-zero.","commonSituations":"A currency was added but its exchange rate never populated; an admin reset rates; a manual rate entry was cleared; import job wrote Rate=0.","solutions":["Set a non-zero Rate for the source currency in Admin > Configuration > Currencies (or trigger a live-rate refresh).","Validate currency.Rate > 0 before invoking the conversion.","Skip conversion when amount is zero (the method already short-circuits this) — but fix the underlying rate for non-zero amounts."],"exampleFix":"// before\nvar value = await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, sourceCurrency);\n\n// after\nif (sourceCurrency.Rate == decimal.Zero)\n    throw new InvalidOperationException($\"Currency '{sourceCurrency.Name}' has no exchange rate set.\");\nvar value = await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, sourceCurrency);","handlingStrategy":"validation","validationCode":"if (sourceCurrency.Rate == decimal.Zero)\n    throw new InvalidOperationException($\"Currency '{sourceCurrency.Name}' has no rate.\");","typeGuard":"static bool HasRate(Currency c) => c is not null && c.Rate != decimal.Zero;","tryCatchPattern":"try { await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, src); }\ncatch (NopException ex) when (ex.Message.Contains(\"Exchange rate not found\"))\n{ _logger.LogWarning(ex, \"Missing rate; skipping conversion.\"); }","preventionTips":["Populate Rate for every active currency.","Run periodic live-rate refreshes.","Validate Rate > 0 before conversion calls."],"tags":["currency","exchange-rate","data","configuration"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}