diff --git a/app/Livewire/Public/StationSearch.php b/app/Livewire/Public/StationSearch.php index 6d9751c..1f035b6 100644 --- a/app/Livewire/Public/StationSearch.php +++ b/app/Livewire/Public/StationSearch.php @@ -2,6 +2,7 @@ namespace App\Livewire\Public; +use Illuminate\Http\Client\ConnectionException; use Illuminate\Support\Facades\Http; use Illuminate\View\View; use Livewire\Attributes\Validate; @@ -34,14 +35,20 @@ class StationSearch extends Component $radiusKm = round($this->radius * 1.60934, 2); - $response = Http::timeout(10) - ->withHeaders(['X-Api-Key' => config('services.fuelalert.api_key')]) - ->get(url('/api/stations'), [ - 'postcode' => $this->search, - 'fuel_type' => $this->fuelType, - 'radius' => $radiusKm, - 'sort' => 'price', - ]); + try { + $response = Http::timeout(10) + ->withHeaders(['X-Api-Key' => config('app.api_secret_key')]) + ->get(url('/api/stations'), [ + 'postcode' => $this->search, + 'fuel_type' => $this->fuelType, + 'radius' => $radiusKm, + 'sort' => 'price', + ]); + } catch (ConnectionException) { + $this->apiError = 'Unable to fetch stations. Please try again.'; + + return; + } if ($response->status() === 422) { $errors = $response->json('errors', []);