From 19cac586927d90dee8283fbe292037c6f0a9bcb9 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Thu, 28 Aug 2025 16:01:44 -0500 Subject: [PATCH] Fix: do not gracefully fail when symbol not found --- app/Interfaces/MarketData/AlpacaMarketData.php | 7 ++++--- app/Interfaces/MarketData/AlphaVantageMarketData.php | 2 +- app/Interfaces/MarketData/TwelveDataMarketData.php | 11 ++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Interfaces/MarketData/AlpacaMarketData.php b/app/Interfaces/MarketData/AlpacaMarketData.php index 0cd7f2d..d5f7a8c 100644 --- a/app/Interfaces/MarketData/AlpacaMarketData.php +++ b/app/Interfaces/MarketData/AlpacaMarketData.php @@ -14,6 +14,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class AlpacaMarketData implements MarketDataInterface { @@ -51,9 +52,7 @@ class AlpacaMarketData implements MarketDataInterface $quote = $response->json('trade'); - if (is_null(Arr::get($quote, 'p'))) { - throw new \Exception('Could not find ticker on Alpaca'); - } + throw_if(empty(Arr::get($quote, 'p')), NotFoundHttpException::class, "Symbol `{$symbol}` was not found"); $fundamental = cache()->remember( 'ap-symbol-'.$symbol, @@ -159,6 +158,8 @@ class AlpacaMarketData implements MarketDataInterface $history = $response->json('bars'); + throw_if(empty($history), NotFoundHttpException::class, "Symbol `{$symbol}` was not found"); + $chunkedHistory = collect($history) ->mapWithKeys(function ($history) use ($symbol) { diff --git a/app/Interfaces/MarketData/AlphaVantageMarketData.php b/app/Interfaces/MarketData/AlphaVantageMarketData.php index 94f809e..701203a 100644 --- a/app/Interfaces/MarketData/AlphaVantageMarketData.php +++ b/app/Interfaces/MarketData/AlphaVantageMarketData.php @@ -145,7 +145,7 @@ class AlphaVantageMarketData implements MarketDataInterface return [$date => new Ohlc([ 'symbol' => $symbol, 'date' => $date, - 'close' => Arr::get($history, '4. close'), + 'close' => (float) Arr::get($history, '4. close'), ])]; }); } diff --git a/app/Interfaces/MarketData/TwelveDataMarketData.php b/app/Interfaces/MarketData/TwelveDataMarketData.php index df7207c..8e2c2a3 100644 --- a/app/Interfaces/MarketData/TwelveDataMarketData.php +++ b/app/Interfaces/MarketData/TwelveDataMarketData.php @@ -13,6 +13,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class TwelveDataMarketData implements MarketDataInterface { @@ -53,9 +54,7 @@ class TwelveDataMarketData implements MarketDataInterface $quote = $response->json(); - if (! isset($quote['price'])) { - throw new \Exception('Could not find ticker on Twelve Data'); - } + throw_if(empty(Arr::get($quote, 'price')), NotFoundHttpException::class, "Symbol `{$symbol}` was not found"); $current_market_value = Arr::get($quote, 'price'); @@ -152,9 +151,11 @@ class TwelveDataMarketData implements MarketDataInterface ]) ->get('time_series'); - $values = $response->json('values'); + $history = $response->json('values'); - return collect($values) + throw_if(empty($history), NotFoundHttpException::class, "Symbol `{$symbol}` was not found"); + + return collect($history) ->mapWithKeys(function ($history) use ($symbol) { $date = Carbon::parse(Arr::get($history, 'datetime'))->toDateString();