Fix: do not gracefully fail when symbol not found

This commit is contained in:
hackerESQ
2025-08-28 16:01:44 -05:00
parent 7d77b6fbc8
commit 19cac58692
3 changed files with 11 additions and 9 deletions
@@ -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) {
@@ -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'),
])];
});
}
@@ -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();