diff --git a/app/Services/CurrencyConversionService.php b/app/Services/CurrencyConversionService.php index 8dfa2997..94673b04 100644 --- a/app/Services/CurrencyConversionService.php +++ b/app/Services/CurrencyConversionService.php @@ -86,6 +86,11 @@ class CurrencyConversionService foreach ($this->rateUrls($currency, $candidateDate) as $url) { try { $response = Http::timeout(10)->get($url); + + if ($response->notFound()) { + continue; + } + $response->throw(); return $response->json($currency) ?? []; @@ -95,7 +100,16 @@ class CurrencyConversionService } } - throw new RuntimeException("Failed to fetch currency rates for {$currency} on {$date}: {$lastException?->getMessage()}", 0, $lastException); + if ($lastException !== null) { + throw new RuntimeException("Failed to fetch currency rates for {$currency} on {$date}: {$lastException->getMessage()}", 0, $lastException); + } + + Log::warning('Currency rates unavailable, all sources returned 404', [ + 'currency' => $currency, + 'date' => $date, + ]); + + return []; } /** diff --git a/tests/Feature/CurrencyConversionServiceTest.php b/tests/Feature/CurrencyConversionServiceTest.php index 94067c47..aa3581c2 100644 --- a/tests/Feature/CurrencyConversionServiceTest.php +++ b/tests/Feature/CurrencyConversionServiceTest.php @@ -73,6 +73,18 @@ test('throws when both primary and fallback fail', function () { $service->convert('BTC', 'EUR', 1.0, '2026-01-15'); })->throws(RuntimeException::class); +test('returns zero when base currency is unknown and all sources return 404', function () { + Http::fake([ + 'cdn.jsdelivr.net/*' => Http::response('Not Found', 404), + 'currency-api.pages.dev/*' => Http::response('Not Found', 404), + ]); + + $service = new CurrencyConversionService; + $result = $service->convert('BTC', 'XXX', 1.0, '2026-01-15'); + + expect($result)->toBe(0.0); +}); + test('falls back to previous historical date when requested release is missing', function () { Http::fake([ 'cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@2025-12-10/*' => Http::response('Not Found', 404),