From 68dd3dd2f89f6cb0cac81a8719cc75a27ad68f81 Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Tue, 18 Jun 2024 12:31:21 +0200 Subject: [PATCH] fix: Handle null response in SaleController index method - Add try-catch block to handle null response from API - Log errors and return appropriate HTTP responses - Update error message in 503.blade.php to show exception message if available --- app/Http/Controllers/SaleController.php | 32 +++++++++++++++++++------ resources/views/errors/503.blade.php | 2 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/SaleController.php b/app/Http/Controllers/SaleController.php index 8afc759..20fcfba 100644 --- a/app/Http/Controllers/SaleController.php +++ b/app/Http/Controllers/SaleController.php @@ -9,15 +9,33 @@ class SaleController extends Controller { public function index() { - $sales = Cache::remember('sales_data', 60 * 60 * 8, static function () { - $lmi_api_key = config('services.lmi.api_key'); + try { + $sales = Cache::remember('sales_data', 60 * 60 * 8, static function () { + $lmi_api_key = config('services.lmi.api_key'); - $response = Http::withHeaders([ - 'Authorization' => 'Bearer '.$lmi_api_key, - ])->get('https://lmi.orianna.dev/api/lol-sales'); + $response = Http::withHeaders([ + 'Authorization' => 'Bearer ' . $lmi_api_key, + ])->get('https://lmi.orianna.dev/api/lol-sales'); + + $response = $response->json(); + + if ($response['champion_sales'] === null) { + logger()->error('LMI has broken'); + return abort(503, 'Trying to access array offset on value of type null'); + } + + return $response; + }); + } catch (\Exception $e) { + if ($e->getMessage() === 'Trying to access array offset on value of type null') { + logger()->error('LMI has broken'); + abort(503, 'Sorry, the Sale Rotation is currently under maintenance. Please try again later.'); + } else { + logger()->error('An error occurred while trying to fetch the Sale Rotation', ['error' => $e->getMessage()]); + abort(500, 'Sorry, an error occurred while trying to fetch the Sale Rotation. Please try again later.'); + } + } - return $response->json(); - }); return view('sales.index', ['sales' => $sales]); } diff --git a/resources/views/errors/503.blade.php b/resources/views/errors/503.blade.php index afc3417..200dc31 100644 --- a/resources/views/errors/503.blade.php +++ b/resources/views/errors/503.blade.php @@ -9,7 +9,7 @@

503

Unavailable

-

The page you are looking for is unavailable.

+

{{ $exception->getMessage() ?? 'The page you are looking for is unavailable or under maintenance.' }}

Go back to the homepage